-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mypy handling initializing Pydantic models with dict values incorrectly #17244
Labels
bug
mypy got something wrong
Comments
dpitch40
changed the title
Mypy handling Pydantic discriminated unions incorrectly
Mypy handling initializing Pydantic models with dict values incorrectly
May 14, 2024
I can also reproduce it with this code from the Pydantic docs. from typing import List, Optional
from pydantic import BaseModel
class Foo(BaseModel):
count: int
size: Optional[float] = None
class Bar(BaseModel):
apple: str = 'x'
banana: str = 'y'
class Spam(BaseModel):
foo: Foo
bars: List[Bar]
m = Spam(foo={'count': 4}, bars=[{'apple': 'x1'}, {'apple': 'x2'}]) The Pydantic docs appear to treat code initializing nested classes using dictionaries containing the fields and values as correct (and the code does work for me if I run it), but Mypy is flagging such code as incorrect. |
mypy isn't going to support this. I think pydantic has a mypy plugin that might help with this, you could ask on their repo. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
Mypy is flagging code pasted directly from the Pydantic docs (involving initializing models with dictionary values for nested classes) as incorrectly typed.
To Reproduce
Save the following code (directly from the current Pydantic docs to a Python file.
Then run mypy on it.
Expected Behavior
Mypy should validate the above code without errors, as it is example code directly from Pydantic's docs.
Actual Behavior
Your Environment
mypy.ini
(and other config files): mypy.iniThe text was updated successfully, but these errors were encountered: