From c083ca8e77a6fa36d9422361aa583bb3b402fc12 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 4 Sep 2024 03:15:38 +0200 Subject: [PATCH] Improve and document workarounds for `field_type` in `_collect_type()` --- src/dataclass_binder/_impl.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dataclass_binder/_impl.py b/src/dataclass_binder/_impl.py index d515e91..60ede50 100644 --- a/src/dataclass_binder/_impl.py +++ b/src/dataclass_binder/_impl.py @@ -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. @@ -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") @@ -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__}'")