-
Sorry, I could not really find an answer - just that something very similar was fixed (e.g. #3617) Example Code: from pydantic import BaseModel, Field
class User1(BaseModel):
name: str | None = Field(None)
class User2(BaseModel):
name: str | None = Field(default=None)
u1 = User1() # mypy & pyright error: Missing named argument "name" for "User1" [call-arg]
u2 = User2() # no typing error The example works either way, but both pyright and mypy report a typing error. Is this by design? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Pydantic relies on It is thus recommended to use the explicit |
Beta Was this translation helpful? Give feedback.
Pydantic relies on
PEP 681
to make type checkers aware of the signature of models. The PEP requiresField
arguments to be keyword only, butdefault
can be set as a positional argument for historical reasons (it was here before PEP 681 was a thing, and was kept as is for compatibility reasons).It is thus recommended to use the explicit
default
keyword argument, or, as an alternative,name: str | None = None
(or usingAnnotated
, see #7379 (reply in thread)).