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 issues with BaseSettings and SettingsConfigDict - v2 #95

Closed
axeng opened this issue Jul 2, 2023 · 1 comment · Fixed by pydantic/pydantic#6396
Closed

Mypy issues with BaseSettings and SettingsConfigDict - v2 #95

axeng opened this issue Jul 2, 2023 · 1 comment · Fixed by pydantic/pydantic#6396
Assignees

Comments

@axeng
Copy link

axeng commented Jul 2, 2023

It seems that mypy has some issues with the new BaseSettings.
I did the same tests with pydantic v1 and it did not throw these errors.

Using this example:

from pydantic_settings import BaseSettings, SettingsConfigDict

class TestSettings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")

    test_field: str

settings = TestSettings()

Mypy is throwing the following:

test.py:4: error: Incompatible types (expression has type "str", TypedDict item "env_file" has type "Path | list[Path] | tuple[Path, ...] | None")  [typeddict-item]
test.py:8: error: Missing named argument "test_field" for "TestSettings"  [call-arg]
Found 2 errors in 1 file (checked 1 source file)

Here the Mypy configuration I am using:

[tool.mypy]
ignore_missing_imports = true
plugins = [
    "pydantic.mypy",
]
disallow_untyped_defs = true
disallow_any_unimported = true
no_implicit_optional = true
check_untyped_defs = true
warn_unused_ignores = true
show_error_codes = true
warn_unreachable = true

pydantic version: 2.0
pydantic-core version: 2.0.1
pydantic-settings version: 2.0
mypy version: 1.4.1

Selected Assignee: @dmontagu

@hramezani
Copy link
Member

Thanks @axeng for reporting this issue.

The first error:

test.py:4: error: Incompatible types (expression has type "str", TypedDict item "env_file" has type "Path | list[Path] | tuple[Path, ...] | None") [typeddict-item]

Can be fixed by including str in the allowed type for env_file config.

The second error:

test.py:8: error: Missing named argument "test_field" for "TestSettings" [call-arg]

There was some logic related to setting management in V1 mypy plugin which has been removed because settings management is not part of Pydantic V2.
I think we can create a mypy plugin for pydantic-settings that inherit from Pydantic V2 mypy plugin and add specific logic for pydantic-settings.

What do you think @samuelcolvin @dmontagu ?

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

Successfully merging a pull request may close this issue.

3 participants