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 error when initializing from a dictionary #513

Open
mikeoconnor0308 opened this issue Jan 2, 2025 · 1 comment
Open

Mypy error when initializing from a dictionary #513

mikeoconnor0308 opened this issue Jan 2, 2025 · 1 comment

Comments

@mikeoconnor0308
Copy link

Migrating from pydantic V1 BaseSettings to the pydantic_settings module, I encountered the following mypy issue when instantiating a settings object from a dictionary:

from pydantic_settings import BaseSettings

class MySettings(BaseSettings):
    FOO: str = "default"
    BAR: int = 123

settings = {"FOO": "foo", "BAR": 456}

my_settings = MySettings(**settings)

running mypy on this snippet produces:

base_settings_mypy.py:9: error: Argument 1 to "MySettings" has incompatible type "**dict[str, object]"; expected "bool | None"  [arg-type]
base_settings_mypy.py:9: error: Argument 1 to "MySettings" has incompatible type "**dict[str, object]"; expected "str | None"  [arg-type]
base_settings_mypy.py:9: error: Argument 1 to "MySettings" has incompatible type "**dict[str, object]"; expected "Path | str | Sequence[Path | str] | None"  [arg-type]
base_settings_mypy.py:9: error: Argument 1 to "MySettings" has incompatible type "**dict[str, object]"; expected "bool | list[str] | tuple[str, ...] | None"  [arg-type]
base_settings_mypy.py:9: error: Argument 1 to "MySettings" has incompatible type "**dict[str, object]"; expected "CliSettingsSource[Any] | None"  [arg-type]
Found 5 errors in 1 file (checked 1 source file)

This works fine in pydantic V1.

Version info (conda forge)

# Name                    Version                   Build  Channel
mypy                      1.14.1          py311h917b07b_0    https://conda.anaconda.org/conda-forge
pydantic                  2.10.4             pyh3cfb1c2_0    https://conda.anaconda.org/conda-forge
pydantic-core             2.27.2          py311h3ff9189_0    https://conda.anaconda.org/conda-forge
pydantic-settings         2.7.1              pyh3cfb1c2_0    https://conda.anaconda.org/conda-forge
@hramezani
Copy link
Member

@dmontagu do you have time to take a look at this?

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

No branches or pull requests

3 participants