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 "Explicit "Any" is not allowed" error when extending BaseModel #9373

Open
1 task done
DetachHead opened this issue May 2, 2024 · 2 comments
Open
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@DetachHead
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

in my codebase, we ban the Any type using the --disallow-any-explicit rule (along with all the other rules that ban Any in other contexts). when extending BaseModel, mypy complains with a no-any-explicit error.

this is annoying because it means we have to add a type:ignore comment on all of our classes that use BaseModel.

related issue: the dataclass decorator had the same problem (#4355), which i fixed in #4356. but i can't seem to figure out what's causing it in this case. i think it may be related to the pydantic mypy plugin?

Example Code

from pydantic import BaseModel

class Foo(BaseModel): # error: Explicit "Any" is not allowed (no-any-explicit)
    ...

Python, Pydantic & OS Version

pydantic version: 2.5.3
        pydantic-core version: 2.14.6
          pydantic-core build: profile=release pgo=true
                 install path: C:\Users\user\project\.venv\Lib\site-packages\pydantic
               python version: 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
                     platform: Windows-11-10.0.22621-SP0
             related packages: typing_extensions-4.11.0
@DetachHead DetachHead added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 2, 2024
@DetachHead
Copy link
Contributor Author

DetachHead commented May 2, 2024

BaseModels with {"extra": "forbid"} do not have this issue:

class Foo(BaseModel): # no error
    model_config = ConfigDict(extra="forbid")

however, the mypy plugin does not seem to work when the config comes from a variable:

from pydantic import BaseModel, ConfigDict

default_config = ConfigDict(extra="forbid")

class Foo(BaseModel): # error: Explicit "Any" is not allowed (no-any-explicit)
    model_config = default_config

edit:

actually, even with {"extra": "forbid"}, they're still broken if there are any fields on them at all:

class Foo(BaseModel): # error
    model_config = ConfigDict(extra="forbid")
    foo: int # error goes away if you remove this

edit 2:

seems like configuring the mypy plugin with these options fixes it:

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true

@sydney-runkle
Copy link
Member

@DetachHead,

Hmm, thanks for reporting this. @dmontagu might have a better idea of how the mypy plugin relates to this issue. Let's see what he thinks!

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 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants