Skip to content
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 type checking not working properly for classes that inherit properties after v2.6.4 #9007

Closed
1 task done
Tracked by #8993
pengelsman7 opened this issue Mar 13, 2024 · 5 comments · Fixed by #9008
Closed
1 task done
Tracked by #8993
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@pengelsman7
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In v2.6.3 I had lines of code that rightfully needed type checking or mypy ignores for union-attr since the fields were typed like <type> | None. However, after I upgraded to v2.6.4 mypy is now telling me many of these type ignores are no longer necessary even though I didn't change my code at all. It seems to only be happening for classes that inherit from another model as you can see from my example. Anything that is just a BaseModel class seems to still require the type checking. I imagine its probably related to the change in this PR?

Example Code

from pydantic import BaseModel


class Foo(BaseModel):
    id: int | None


class Bar(BaseModel):
    foo: Foo | None


class Baz(Bar):
    name: str


b = Bar(foo={"id": 1})
z = Baz(foo={"id": 1}, name="test")


assert z.foo.id == 1  # needed an ignore before
assert b.foo.id == 1  # type: ignore[union-attr]

Python, Pydantic & OS Version

pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=false
                 install path: /fn/lib/venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Mar 10 2024, 01:11:42) [GCC 10.2.1 20210110]
                     platform: Linux-6.5.13-orbstack-00122-g57b8027e2387-aarch64-with-glibc2.31
             related packages: typing_extensions-4.9.0
                       commit: unknown
@pengelsman7 pengelsman7 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 13, 2024
@dmontagu
Copy link
Contributor

dmontagu commented Mar 13, 2024

Can you share your mypy config? Are you using no_strict_optional = True?

Also, what version of mypy are you using?

@pengelsman7
Copy link
Author

I am using mypy v1.9.0 and I am actually using strict_optional=true. Here is the whole config:

[tool.mypy]
pretty = true

color_output = true

show_column_numbers = true
show_error_context = true
show_error_codes = true

warn_redundant_casts = true
warn_unused_ignores = true
warn_unreachable = true

check_untyped_defs = true
no_implicit_optional = true
strict_optional = true

disallow_untyped_calls = true
disallow_untyped_defs = true

plugins = ["pydantic.mypy"]

@dmontagu
Copy link
Contributor

Thank you for that info @pengelsman7, I was able to confirm locally that #9008 seems to fix your issue. It would be great if you could confirm if there are still any misbehaviors not addressed by that PR but even if you can't I think we'll merge and release it soon since it at least fixes some bugs.

@pengelsman7
Copy link
Author

I just confirmed that your PR fixed all the issues I saw, thanks so much for the quick turn around!

@sydney-runkle
Copy link
Member

Awesome! We'll get #9008 across the line before the 2.7.0b1 release, so this should be fixed soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants