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 "BaseSettings" #11313

Open
1 task done
denisborisov opened this issue Jan 22, 2025 · 4 comments · May be fixed by pydantic/pydantic-settings#529
Open
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@denisborisov
Copy link

denisborisov commented Jan 22, 2025

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.
When extending BaseSettings, mypy complains with an error: Explicit "Any" is not allowed.

This is annoying because it means we have to add a type: ignore[misc] comment on all of our classes that use BaseSettings.

Related issue: #9373 regarding the BaseModel, that has already been fixed.

Example Code

import pydantic_settings

class Settings(pydantic_settings.BaseSettings):
    pass

Python, Pydantic & OS Version

pydantic version: 2.10.5
        pydantic-core version: 2.27.2
          pydantic-core build: profile=release pgo=false
                 install path: /Users/denisborisov/Downloads/sandbox/bug/.venv/lib/python3.13/site-packages/pydantic
               python version: 3.13.1 (main, Dec 22 2024, 13:45:29) [Clang 16.0.0 (clang-1600.0.26.6)]
                     platform: macOS-15.2-arm64-arm-64bit-Mach-O
             related packages: mypy-1.14.1 pydantic-settings-2.7.1 typing_extensions-4.12.2
                       commit: unknown
@denisborisov denisborisov added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Jan 22, 2025
@denisborisov
Copy link
Author

denisborisov commented Jan 22, 2025

The correnponding pyproject.toml file:

[build-system]

requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]

package-mode = false

[tool.poetry.dependencies]

pydantic-settings = "2.7.1"
python = "3.13.1"

[tool.poetry.group.dev.dependencies]

mypy = "1.14.1"

[tool.mypy]

plugins = ["pydantic.mypy"]
python_version = "3.13"
disallow_any_explicit = true

[tool.pydantic-mypy]

init_forbid_extra = true
init_typed = true

@Viicos
Copy link
Member

Viicos commented Jan 22, 2025

See the outcome of the issue you mentioned (the added note can be found at the end of the mypy plugin documentation).

Alternatively, we could switch to object instead in Any in the mypy plugin when we synthesize the __init__ methods. PR welcomed; this might break existing setups so we'll have to make sure this works as expected.

@Viicos Viicos closed this as completed Jan 22, 2025
@denisborisov
Copy link
Author

@Viicos

I checked the added note from the end of the mypy plugin documentation.

The problem is that even with enabled init_forbid_extra and init_typed, I still get the error from mypy.

@denisborisov
Copy link
Author

@Viicos , could you reopen the issue, please?

@Viicos Viicos removed the pending Is unconfirmed label Jan 24, 2025
@Viicos Viicos self-assigned this Jan 24, 2025
@Viicos Viicos added this to the v2.11 milestone Jan 24, 2025
@Viicos Viicos reopened this Jan 24, 2025
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants