Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avro dataclass introspect typing #976

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/karapace/avro_dataclasses/introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ def _field_type_array(field: Field, origin: type, type_: object) -> AvroType:
else:
(inner_type,) = get_args(type_)

items: AvroType
if is_dataclass(inner_type):
assert isinstance(inner_type, type)
items = record_schema(inner_type)
else:
items = _field_type(field, inner_type)

return {
"name": f"one_of_{field.name}",
"type": "array",
"items": (record_schema(inner_type) if is_dataclass(inner_type) else _field_type(field, inner_type)),
"items": items,
}


Expand Down Expand Up @@ -128,7 +135,7 @@ def _field_type(field: Field, type_: object) -> AvroType: # pylint: disable=too
T = TypeVar("T", str, int, bool, Enum, None)


def transform_default(type_: type[T], default: T) -> str | int | bool | None:
def transform_default(type_: type[T] | str, default: T) -> str | int | bool | None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the failure case that necessitates the addition of str here? Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pure typing correction. It looks that primitive types use string instead of type[T].

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking this to here:

https://github.com/python/typeshed/blob/6feca188689e14cdacd8dbf8bc9cf0635993027a/stdlib/dataclasses.pyi#L111

When originally authored, this type was type, only changed recently and I guess propagated in most recent mypy bump:

python/typeshed@6b2033a

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_field_type doesn't give a type error because it accepts object and makes no assumptions. It's probably somewhat likely to break when the new deferred annotations arrive, but I wouldn't deal with that until it's possible to test with.

if isinstance(default, Enum):
assert isinstance(type_, type)
assert issubclass(type_, Enum)
Expand Down
Loading