Skip to content

Commit

Permalink
fixup! fix _generic_type_add_any
Browse files Browse the repository at this point in the history
  • Loading branch information
anis-campos committed Sep 6, 2023
1 parent 1b4fb02 commit d2f1a6f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions marshmallow_dataclass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d2f1a6f

Please sign in to comment.