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

Overridden untyped ClassVar get an error_untyped_fields with mypy plugin #8107

Closed
1 task done
vincent-hachin-wmx opened this issue Nov 14, 2023 · 3 comments · Fixed by #8138
Closed
1 task done

Overridden untyped ClassVar get an error_untyped_fields with mypy plugin #8107

vincent-hachin-wmx opened this issue Nov 14, 2023 · 3 comments · Fixed by #8138
Assignees
Labels
bug V2 Bug related to Pydantic V2 help wanted Pull Request welcome mypy related to mypy

Comments

@vincent-hachin-wmx
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Untyped overridden fields are forbidden and an error is thrown on execution and in mypy. However, untyped ClassVar are allowed in a BaseModel, but it seems that this case is not properly handeled by the mypy plugin here .

The example code below run fine, but a mypy error error: Untyped fields disallowed is thrown.

Example Code

from typing import ClassVar
from pydantic import BaseModel

class Abstract(BaseModel):
    class_id: ClassVar

class Foo(Abstract):
    class_id = 1

print(Foo())

Python, Pydantic & OS Version

pydantic version: 2.5.0
        pydantic-core version: 2.14.1
          pydantic-core build: profile=release pgo=true
                 install path: /Users/vincent.hachin/.pyenv/versions/3.11.6/lib/python3.11/site-packages/pydantic
               python version: 3.11.6 (main, Oct  9 2023, 17:58:53) [Clang 15.0.0 (clang-1500.0.40.1)]
                     platform: macOS-14.0-arm64-arm-64bit
             related packages: typing_extensions-4.8.0 mypy-1.7.0
@vincent-hachin-wmx vincent-hachin-wmx added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Nov 14, 2023
@sydney-runkle sydney-runkle added mypy related to mypy and removed pending Awaiting a response / confirmation labels Nov 14, 2023
@sydney-runkle sydney-runkle self-assigned this Nov 14, 2023
@sydney-runkle
Copy link
Member

Hi @vincent-hachin-wmx,

Thanks for reporting this. This does look like a bug. Looks like you may have found the source of the problem. Are you interested in contributing a PR with a fix?

@vincent-hachin-wmx
Copy link
Contributor Author

Hi,
Sure I'll do a PR.

@vincent-hachin-wmx
Copy link
Contributor Author

Hi @sydney-runkle,

I made a PR but I couldn't figure out how to add unit tests for the mypy plugin.
I guess the test should be added around here:

class DefaultTestingModel(BaseModel):
# Required
a: int
b: int = ...
c: int = Field(...)
d: Union[int, str]
e = ...
# Not required
f: Optional[int]
g: int = 1
h: int = Field(1)
i: int = Field(None)
j = 1

How to specify that a line should pass or throw a specific mypy error ?

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 help wanted Pull Request welcome mypy related to mypy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants