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 thinks a config variable can never be None when nested in inherited BaseModel class, again #9068

Closed
1 task done
Greesb opened this issue Mar 21, 2024 · 1 comment
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2 mypy related to mypy

Comments

@Greesb
Copy link

Greesb commented Mar 21, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hello,

mypy thinks that some variable can never be None when the typing and the default value clearly indicate otherwise.

Was already declared for 2.3 #7399, fixed until 2.6.3 and it seems to be back again for version 2.6.4.

Example Code

import pydantic


class ModelNested(pydantic.BaseModel):
    my_var: str | None = pydantic.Field(default=None)


class Model(ModelNested):
    pass


m = Model()
if m.my_var is None:
    print("hello there")  # test.py:14: error: Statement is unreachable  [unreachable]

Python, Pydantic & OS Version

pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=true
                 install path: ...
               python version: 3.12.2 (main, Feb 14 2024, 11:13:34) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.4-arm64-arm-64bit
             related packages: fastapi-0.110.0 mypy-1.9.0 typing_extensions-4.10.0 pydantic-settings-2.2.1
                       commit: unknown

mypy settings

[tool.mypy]
strict = true
warn_unreachable = true
exclude = [".venv"]
show_error_codes = true
plugins = ["pydantic.mypy"]

[[tool.mypy.overrides]]
module = [
       "deepdiff.*",
       "vcr.*",
       "voluptuous.*",
       "msgpack.*",
       "bs4.*",
       "graphql_utils",
       "markdownify",
       "asgi_lifespan",
       "authlib.*",
       "imia.*",
       "starsessions.*",
       "sqlalchemy_utils.*",
       "alembic_utils.*",
       "google.*",
]
ignore_missing_imports = true
@Greesb Greesb added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 21, 2024
@sydney-runkle
Copy link
Member

Should be fixed in 2.7, coming out soon! Fixed by @dmontagu here: #9008

@sydney-runkle sydney-runkle added mypy related to mypy bug V2 Bug related to Pydantic V2 and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 22, 2024
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 mypy related to mypy
Projects
None yet
Development

No branches or pull requests

3 participants