Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
751 stars 44 forks source link

Add support for PEP 696 #216

Closed Fatal1ty closed 3 months ago

Fatal1ty commented 4 months ago

Is your feature request related to a problem? Please describe. PEP 695 introduces the concept of type defaults for type parameters, including TypeVar and TypeVarTuple, which act as defaults for type parameters for which no type is specified. We can use this new information in the same way as we do with bound parameter.

Describe the solution you'd like Use the new __default__ parameter on a par with the __bound__ parameter giving priority to the first.

The following examples will be equivalent (we will assume x to be int):

T = TypeVar("T", default=int)

@dataclass
class MyClass(Generic[T]):
    x: T
T = TypeVar("T", bound=int)

@dataclass
class MyClass(Generic[T]):
    x: T
@dataclass
class MyClass[T: int]:
    x: T

Additional context We should support this new PEP for TypeVarTuple as well. All the examples from PEP 696 can be used in new tests.