Skip to content

Commit

Permalink
Improve and document workarounds for field_type in _collect_type()
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuurne committed Sep 4, 2024
1 parent 0ba32ad commit c083ca8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/dataclass_binder/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import tomllib # pragma: no cover


# Note: Actually 'field_type' can either be a type of a typing special form,
# but there is no way yet to annotate typing special forms.
# This is the source of a lot of the casts and suppressions in this function.
def _collect_type(field_type: type, context: str) -> type | Binder[Any]:
"""
Verify and streamline a type annotation.
Expand All @@ -45,7 +48,7 @@ def _collect_type(field_type: type, context: str) -> type | Binder[Any]:
"""
origin = get_origin(field_type)
if origin is None:
if field_type is Any:
if field_type is Any: # type: ignore[comparison-overlap]
return object
elif not isinstance(field_type, type):
raise TypeError(f"Annotation for field '{context}' is not a type")
Expand Down Expand Up @@ -105,7 +108,7 @@ def _collect_type(field_type: type, context: str) -> type | Binder[Any]:
for base in bases:
if not isinstance(base, type):
raise TypeError(f"type[...] annotation for '{context}' must have a type as its argument")
collected_types.append(type[base])
collected_types.append(cast(type[Any], type[base]))
return reduce(operator.__or__, collected_types)
else:
raise TypeError(f"Field '{context}' has unsupported generic type '{origin.__name__}'")
Expand Down

0 comments on commit c083ca8

Please sign in to comment.