Skip to content

Commit c083ca8

Browse files
committed
Improve and document workarounds for field_type in _collect_type()
1 parent 0ba32ad commit c083ca8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/dataclass_binder/_impl.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import tomllib # pragma: no cover
3535

3636

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

0 commit comments

Comments
 (0)