Skip to content

Commit

Permalink
Initial support for PEP 696
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Apr 28, 2024
1 parent af0bcda commit 84f7346
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion mashumaro/core/meta/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ def type_name(
)
return f"{_typing_name('Union', short)}[{args_str}]"
else:
bound = getattr(typ, "__bound__")
bound = getattr(typ, "__default__", None)
if bound is None:
bound = getattr(typ, "__bound__")
return type_name(bound, short, resolved_type_params)
elif is_new_type(typ) and not PY_310_MIN:
# because __qualname__ and __module__ are messed up
Expand Down Expand Up @@ -421,6 +423,8 @@ def is_type_var_any(typ: Type) -> bool:
return False
elif typ.__bound__ not in (None, Any):
return False
elif getattr(typ, "__default__", None) not in (None, NoneType):
return False
else:
return True

Expand Down
4 changes: 3 additions & 1 deletion mashumaro/core/meta/types/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ def pack_special_typing_primitive(spec: ValueSpec) -> Optional[Expression]:
if constraints:
return pack_union(spec, constraints, "type_var")
else:
bound = getattr(spec.type, "__bound__")
bound = getattr(spec.type, "__default__", None)
if bound is None:
bound = getattr(spec.type, "__bound__")
# act as if it was Optional[bound]
pv = PackerRegistry.get(spec.copy(type=bound))
return expr_or_maybe_none(spec, pv)
Expand Down
4 changes: 3 additions & 1 deletion mashumaro/core/meta/types/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ def unpack_special_typing_primitive(spec: ValueSpec) -> Optional[Expression]:
if constraints:
return TypeVarUnpackerBuilder(constraints).build(spec)
else:
bound = getattr(spec.type, "__bound__")
bound = getattr(spec.type, "__default__", None)
if bound is None:
bound = getattr(spec.type, "__bound__")
# act as if it was Optional[bound]
uv = UnpackerRegistry.get(spec.copy(type=bound))
return expr_or_maybe_none(spec, uv)
Expand Down
5 changes: 3 additions & 2 deletions tests/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import date, datetime
from enum import Enum, Flag, IntEnum, IntFlag
from os import PathLike
from typing import Any, Generic, List, NewType, Optional, TypeVar, Union
from typing import Any, Generic, List, NewType, Optional, Union

try:
from enum import StrEnum
Expand All @@ -13,7 +13,7 @@ class StrEnum(str, Enum):
pass


from typing_extensions import NamedTuple, TypedDict
from typing_extensions import NamedTuple, TypedDict, TypeVar

from mashumaro import DataClassDictMixin
from mashumaro.config import TO_DICT_ADD_OMIT_NONE_FLAG, BaseConfig
Expand All @@ -22,6 +22,7 @@ class StrEnum(str, Enum):
T = TypeVar("T")
TAny = TypeVar("TAny", bound=Any)
TInt = TypeVar("TInt", bound=int)
TDefaultInt = TypeVar("TDefaultInt", default=int)
TIntStr = TypeVar("TIntStr", int, str)
T_Optional_int = TypeVar("T_Optional_int", bound=Optional[int])

Expand Down
6 changes: 6 additions & 0 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
MyUntypedNamedTuple,
T,
TAny,
TDefaultInt,
TInt,
TIntStr,
)
Expand Down Expand Up @@ -165,7 +166,10 @@ def test_is_dataclass_dict_mixin_subclass():


def test_is_type_var_any():
assert is_type_var_any(T)
assert is_type_var_any(TAny)
assert not is_type_var_any(TInt)
assert not is_type_var_any(TDefaultInt)
assert not is_type_var_any(typing.Any)
assert not is_type_var_any(TMyDataClass)

Expand All @@ -180,6 +184,7 @@ def test_is_type_var_any_list_38():
def test_type_name():
assert type_name(TAny) == "typing.Any"
assert type_name(TInt) == "int"
assert type_name(TDefaultInt) == "int"
assert type_name(TMyDataClass) == "tests.entities.MyDataClass"
assert type_name(TIntStr) == "typing.Union[int, str]"
assert type_name(typing.List[TInt]) == "typing.List[int]"
Expand Down Expand Up @@ -285,6 +290,7 @@ def test_type_name_pep_585():
def test_type_name_short():
assert type_name(TAny, short=True) == "Any"
assert type_name(TInt, short=True) == "int"
assert type_name(TDefaultInt, short=True) == "int"
assert type_name(TMyDataClass, short=True) == "MyDataClass"
assert type_name(TIntStr, short=True) == "Union[int, str]"
assert type_name(typing.List[TInt], short=True) == "List[int]"
Expand Down

0 comments on commit 84f7346

Please sign in to comment.