Skip to content

Is it intentional that Field(None) leads to typing errors but Field(default=None) does not? #9413

Answered by Viicos
smorokin asked this question in Question
Discussion options

You must be logged in to vote

Pydantic relies on PEP 681 to make type checkers aware of the signature of models. The PEP requires Field arguments to be keyword only, but default 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 using Annotated, see #7379 (reply in thread)).

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@smorokin
Comment options

Answer selected by smorokin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants