Skip to content

Commit 568388c

Browse files
committed
Explicitly annotate the possibility of GenericAlias
Despite describing the type of values, `GenericAlias` is not actually a subclass of `type`.
1 parent e18818c commit 568388c

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/dataclass_binder/_impl.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from inspect import cleandoc, get_annotations, getmodule, getsource, isabstract
2525
from pathlib import Path
2626
from textwrap import dedent
27-
from types import MappingProxyType, ModuleType, NoneType, UnionType
27+
from types import GenericAlias, MappingProxyType, ModuleType, NoneType, UnionType
2828
from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar, Generic, TypeVar, Union, cast, get_args, get_origin, overload
2929
from weakref import WeakKeyDictionary
3030

@@ -37,7 +37,7 @@
3737
# Note: Actually 'field_type' can either be a type of a typing special form,
3838
# but there is no way yet to annotate typing special forms.
3939
# This is the source of a lot of the casts and suppressions in this function.
40-
def _collect_type(field_type: type, context: str) -> type | Binder[Any]:
40+
def _collect_type(field_type: type, context: str) -> type | GenericAlias | Binder[Any]:
4141
"""
4242
Verify and streamline a type annotation.
4343
@@ -215,7 +215,7 @@ class _ClassInfo(Generic[T]):
215215
_cache: ClassVar[MutableMapping[type[Any], _ClassInfo[Any]]] = WeakKeyDictionary()
216216

217217
dataclass: type[T]
218-
field_types: Mapping[str, type | Binder[Any]]
218+
field_types: Mapping[str, type | GenericAlias | Binder[Any]]
219219
_field_docstrings: Mapping[str, str] | None = None
220220

221221
@classmethod
@@ -225,7 +225,7 @@ def get(cls, dataclass: type[T]) -> _ClassInfo[T]:
225225
except KeyError:
226226
# Populate field_types *after* adding new instance to the cache to make sure
227227
# _collect_type() will find the given dataclass if it's accessed recursively.
228-
field_types: dict[str, type | Binder[Any]] = {}
228+
field_types: dict[str, type | GenericAlias | Binder[Any]] = {}
229229
info = cls(dataclass, field_types)
230230
cls._cache[dataclass] = info
231231
for field, field_type in _get_fields(dataclass):
@@ -371,7 +371,9 @@ def _bind_to_single_type(self, value: object, field_type: type, context: str) ->
371371

372372
raise TypeError(f"Value for '{context}' has type '{type(value).__name__}', expected '{field_type.__name__}'")
373373

374-
def _bind_to_field(self, value: object, field_type: type | Binder[Any], instance: T | None, context: str) -> object:
374+
def _bind_to_field(
375+
self, value: object, field_type: type | GenericAlias | Binder[Any], instance: T | None, context: str
376+
) -> object:
375377
"""
376378
Convert a TOML value to a field type which is possibly a union type.
377379
@@ -884,7 +886,7 @@ def format_template(class_or_instance: Any) -> Iterator[str]:
884886
yield from Binder(class_or_instance).format_toml_template()
885887

886888

887-
def _format_value_for_type(field_type: type[Any]) -> str:
889+
def _format_value_for_type(field_type: GenericAlias | type[Any]) -> str:
888890
origin = get_origin(field_type)
889891
if origin is None:
890892
if field_type is str:

0 commit comments

Comments
 (0)