Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Incorrect stub generation for subclasses of pydantic.BaseModel #16968

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

Open
jborman-stonex opened this issue Feb 29, 2024 · 4 comments
Open

Incorrect stub generation for subclasses of pydantic.BaseModel #16968

jborman-stonex opened this issue Feb 29, 2024 · 4 comments
Labels
bug mypy got something wrong

Comments

@jborman-stonex
Copy link

Bug Report

stubgen does not properly generate ellipses for default values set in the scope of the class body of a Pydantic model.

To Reproduce

Running stubgen -m example

# example.py
from pydantic import BaseModel


class Foo(BaseModel):
    abc: int = 1
    xyz: str = "abc"

Should produce

Expected Behavior

# example.pyi
from pydantic import BaseModel

class Foo(BaseModel):
    abc: int = ...
    xyz: str = ...

stubgen should be populating the default annotation with ellipses, instead they are missing.

Actual Behavior

# example.pyi
from pydantic import BaseModel

class Foo(BaseModel):
    abc: int
    xyz: str

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: stubgen -m example
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.11.8
@jborman-stonex jborman-stonex added the bug mypy got something wrong label Feb 29, 2024
@BrunoRomes
Copy link

Seeing the exact same behavior on a different environment:

Mypy version used: mypy 1.9.0 (compiled: yes)
Mypy command-line flags: stubgen -m example
Mypy configuration options from mypy.ini (and other config files):

[tool.mypy]
plugins = "pydantic.mypy"

Python version used: 3.10.13

@ggeorge-pros
Copy link

ggeorge-pros commented May 18, 2024

Also seeing this in my environment. I see it happening regardless if I define the class using either of these two

class A(BaseModel):
    foo: Optional[str] = "bar"

class B(BaseModel):
    foo: Optional[str] = Field(default="bar")

Version information:

$ python --version
3.8.19

$ python -m mypy --version
mypy 1.10.0 (compiled: yes)

$ pip freeze | grep pydantic  
pydantic==2.7.1
pydantic_core==2.18.2

Stubs are created using

$ stubgen -o module_dir module_dir/

and my pyproject.toml has the following:

[tool.mypy]
plugins = ["pydantic.mypy"]

@adxl
Copy link

adxl commented Jun 5, 2024

I don't think it is related to pydantic because it also happens for basic classes like:

# .py
class A:
    foo: str = "bar"

... which generates these stubs:

# .pyi
class A:
    foo: str

mypy 1.10.0 (compiled: yes)
Python 3.12.2

@teplandr
Copy link

teplandr commented May 9, 2025

Hello everyone! I stumbled upon the same issue. Does anyone know a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

5 participants