Skip to content

Incorrect stub generation for subclasses of pydantic.BaseModel #16968

Open
@jborman-stonex

Description

@jborman-stonex

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

Activity

BrunoRomes

BrunoRomes commented on Mar 19, 2024

@BrunoRomes

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

ggeorge-pros commented on May 18, 2024

@ggeorge-pros

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

adxl commented on Jun 5, 2024

@adxl

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

teplandr commented on May 12, 2025

@teplandr

Hi @jborman-stonex! Could you please share an idea on how you resolved this issue in your project?

jborman-stonex

jborman-stonex commented on May 12, 2025

@jborman-stonex
Author

Hi @jborman-stonex! Could you please share an idea on how you resolved this issue in your project?

My solution was manually adding the elipses

linked a pull request that will close this issue on May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @BrunoRomes@adxl@teplandr@ggeorge-pros@jborman-stonex

      Issue actions

        Incorrect stub generation for subclasses of `pydantic.BaseModel` · Issue #16968 · python/mypy