Closed mvanderlee closed 2 months ago
Not a problem, but something to be aware of:
The proposed use of Annotated
types is not equivalent to the old custom NewType
with regards to static type checking. (NewType
introduces a new type, while Annotated
adds annotations to an existing type.)
Consider:
from typing import Annotated
from marshmallow import fields
from marshmallow_dataclass import dataclass, NewType
Email1 = NewType("Email", str, field=fields.Email)
Email2 = Annotated[str, fields.Email]
@dataclass
class Record:
email1: Email1
email2: Email2
r = Record(email1="joe@example.org", email2="jane@example.net")
Mypy
will complain that Argument "email1" to "Record" has incompatible type "str"; expected "Email1"
. Passing the str
for email2
is fine.
Python is updating the Typing spec which causes
Variable not allowed in type expression
for types created with marshmallow_dataclass'sNewType
FWIW: For mypy, at least, use of the marshmallow_dataclass.mypy
mypy plugin fixes this.
Python is updating the Typing spec which causes
Variable not allowed in type expression
for types created with marshmallow_dataclass'sNewType
https://github.com/microsoft/pyright/issues/7268
The modern way is to use
typing.Annotated
. (Python >=3.9)I propose deprecating
NewType
and addingAnnotated
support.Example: