You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When checking Pydantic derived models mypy ignores default values of parent fields #14850
When using Pydantic models, if one creates a parent model with fields with default values, then a child model inheriting from it, and instantiates the child model, mypy erroneously complains for the lack of named arguments corresponding to the parent model's fields.
And it doesn't honor allow_population_by_field_name config anymore.
allow_population_by_field_name:
whether an aliased field may be populated by its name as given by the model attribute, as well as the alias (default: False)
Reacted by Priit Lätt, Wilken Steiner, Bazyli Cyran and jessemyers-lettuce
Pydantic uses dataclass_transform (defined in PEP 681), which mypy now supports. The dataclass_transform mechanism requires you to specify field arguments by keyword because this is how the stdlib dataclass works.
Reacted by Wesley Collin Wright, Paul Morgan, Francisco Rodeño Sánchez, Ryan Andonian, Euxhen Hasanaj, Rastaclaus, Oleg A., Denis Bondar, Gourav Singh Bais, Yuen Hern Yu and 3 more
If you want to restore the previous behavior, pydantic 1.10.6 was released a few hours ago.
Add the pydantic.mypy plugin to your mypy config and it should work.
To elaborate a bit on the comments above: unfortunately some Pydantic features aren't fully covered by the PEP 681 specification, among them the defaults and allow_population_by_field_name. Either you'll need to use the custom plugin that Pydantic provides or you'll have to avoid using these features of Pydantic.
Summary: If you stick with the standard features of dataclass_transform, mypy is able to type check the code without any plugins. If you use more bespoke features of pydantic, then you need to install the pydantic plugin for mypy.
Is there anything actionable remaining in this bug report?
I've spent too much time looking for an answer of my issue after upgrading to pydantic v2.
Since this is the first google result for the query of "pydantic mypy Missing named argument", I feel obligated to add this:
Possible resolvement:
If you are using Optional[type] for any of your fields, make sure you add Field(default=None), as per documentation
Also see pydantic/6713
Pydantic uses dataclass_transform (defined in PEP 681), which mypy now supports. The dataclass_transform mechanism requires you to specify field arguments by keyword because this is how the stdlib dataclass works.
Activity
chassing commentedon Mar 7, 2023
And it doesn't honor
allow_population_by_field_nameconfig anymore.allow_population_by_field_name:
whether an aliased field may be populated by its name as given by the model attribute, as well as the alias (default: False)
Rollback mypy due to python/mypy#14850.
Rollback mypy due to python/mypy#14850.
erictraut commentedon Mar 7, 2023
If you use a keyword argument (
default=) for the field, it works.Pydantic uses
dataclass_transform(defined in PEP 681), which mypy now supports. Thedataclass_transformmechanism requires you to specify field arguments by keyword because this is how the stdlibdataclassworks.Update pylint and puppeteer. (#5588)
Update pylint and puppeteer. (#5588)
cdce8p commentedon Mar 8, 2023
If you want to restore the previous behavior, pydantic
1.10.6was released a few hours ago.Add the
pydantic.mypyplugin to your mypy config and it should work.https://docs.pydantic.dev/mypy_plugin/
Update pylint and puppeteer. (#5588)
wesleywright commentedon Mar 10, 2023
To elaborate a bit on the comments above: unfortunately some Pydantic features aren't fully covered by the PEP 681 specification, among them the defaults and
allow_population_by_field_name. Either you'll need to use the custom plugin that Pydantic provides or you'll have to avoid using these features of Pydantic.Use pydantic mypy plugin and ignore intentional typing errors in tests
erictraut commentedon Aug 14, 2023
Summary: If you stick with the standard features of
dataclass_transform, mypy is able to type check the code without any plugins. If you use more bespoke features of pydantic, then you need to install the pydantic plugin for mypy.Is there anything actionable remaining in this bug report?
irdkwmnsb commentedon Nov 15, 2023
I've spent too much time looking for an answer of my issue after upgrading to pydantic v2.
Since this is the first google result for the query of "pydantic mypy Missing named argument", I feel obligated to add this:
Possible resolvement:
If you are using Optional[type] for any of your fields, make sure you add Field(default=None), as per documentation
Also see pydantic/6713
gouravsinghbais commentedon Jan 11, 2024
This one works pretty well for my case, thanks.