Skip to content

Pydantic overriding default value with Field produces Pyright error, is it expected? #8226

Answered by erictraut
rafrafek asked this question in Q&A
Discussion options

You must be logged in to vote

This is expected behavior. It's neither a bug in pydantic nor in pyright.

Pydantic provides a non-standard way to specify a default value via a positional argument, as you've done in your code sample. Field specifiers in stdlib dataclass do not support this; they require you to specify the default by keyword argument default=.

Standards-compliant type checkers like pyright do not know about pydantics idiosyncrasies. Their behavior is based on the dataclass_transform mechanism which assumes standard dataclass behaviors.

Pydantic also supports the standard dataclass behavior, so if you change your code to the following, it will satisfy both pydantic and standards-based type checkers like py…

Replies: 1 comment 1 reply

Comment options

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

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