diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index 7ad8da5..1cde8a6 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -480,19 +480,35 @@ def _field_by_supertype( ) +COLLECTIONS_TYPES: Dict[str, Set[Type]] = { + "list": {list, List}, + "dict": {dict, Dict}, + "mapping": {Mapping}, + "sequence": {Sequence}, + "set": {set, Set}, + "frozenset": {frozenset, FrozenSet}, +} + +if sys.version_info >= (3, 9): + # supporting PEP 585 collections types + COLLECTIONS_TYPES["mapping"] |= {collections.abc.Mapping} + COLLECTIONS_TYPES["sequence"] |= {collections.abc.Sequence} + COLLECTIONS_TYPES["set"] |= {collections.abc.Set} + + def _generic_type_add_any(typ: type) -> type: """if typ is generic type without arguments, replace them by Any.""" - if typ in (list, List): + if typ in COLLECTIONS_TYPES["list"]: typ = List[Any] - elif typ in (dict, Dict): + elif typ in COLLECTIONS_TYPES["dict"]: typ = Dict[Any, Any] - elif typ in (Mapping, collections.abc.Mapping): + elif typ in COLLECTIONS_TYPES["mapping"]: typ = Mapping[Any, Any] - elif typ in (Sequence, collections.abc.Sequence): + elif typ in COLLECTIONS_TYPES["sequence"]: typ = Sequence[Any] - elif typ in (set, Set, collections.abc.Set): + elif typ in COLLECTIONS_TYPES["set"]: typ = Set[Any] - elif typ in (frozenset, FrozenSet): + elif typ in COLLECTIONS_TYPES["frozenset"]: typ = FrozenSet[Any] return typ