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

Regression on mypy when using pydantic.mypy and a SubModel. #9092

Closed
1 task done
morian opened this issue Mar 24, 2024 · 1 comment
Closed
1 task done

Regression on mypy when using pydantic.mypy and a SubModel. #9092

morian opened this issue Mar 24, 2024 · 1 comment
Labels
bug V2 Bug related to Pydantic V2 mypy related to mypy

Comments

@morian
Copy link
Contributor

morian commented Mar 24, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I noticed a regression between v2.6.3 and v2.6.4, probably due to merge request #8826.
This occurs in the following situation:

  • An optional field is used in a subclass (the optional characteristic is dropped)
  • Pydantic's mypy plugin is used through the toml configuration
  • Strict optional config item is set to true (default behavior)

Result from the example code bellow is:

$ cat pyproject.toml
[tool.mypy]
plugins = ['pydantic.mypy']

$ mypy xxx.py
xxx.py:10: note: Revealed type is "xxx.Sub"
xxx.py:11: note: Revealed type is "builtins.str"
xxx.py:13: error: Incompatible types in assignment (expression has type "None", variable has type "str")  [assignment]

Everything is fine as long as I don't use the plugin, or pydantic v2.6.3, or if I set no_strict_optional, either from the command line or as a configuration entry in the toml file.

Example Code

from pydantic import BaseModel

class Base(BaseModel):
    field: str | None = None

class Sub(Base):
    pass

def stuff(item: Sub) -> None:
    reveal_type(item)
    reveal_type(item.field)

    item.field = None

Python, Pydantic & OS Version

pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=true
                 install path: /tmp/pydantic/venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Mar 24 2024, 10:14:50) [GCC 13.2.1 20240210]
                     platform: Linux-6.8.1-x86_64-Intel-R-_Core-TM-_i7-6700_CPU_@_3.40GHz-with-glibc2.38
             related packages: typing_extensions-4.10.0 mypy-1.9.0
                       commit: unknown
@morian morian added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 24, 2024
@sydney-runkle
Copy link
Member

Indeed, thanks for reporting this. This should be fixed in 2.7 with @dmontagu's recent mypy fixes. Beta for that release should be out soon!

@sydney-runkle sydney-runkle added mypy related to mypy and removed pending Awaiting a response / confirmation labels Mar 25, 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

2 participants