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 complains about settings object #103

Closed
1 task done
sr-verde opened this issue Jul 5, 2023 · 8 comments
Closed
1 task done

mypy complains about settings object #103

sr-verde opened this issue Jul 5, 2023 · 8 comments
Assignees

Comments

@sr-verde
Copy link

sr-verde commented Jul 5, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2 installed directly from the main branch, or equivalent

Description

Mypy complains about my settings object.

$ mypy pydampy
pydampy/config.py:3: error: Cannot find implementation or library stub for module named "pydantic_settings"  [import]
pydampy/config.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
pydampy/config.py:6: error: Class cannot subclass "BaseSettings" (has type "Any")  [misc]
pydampy/config.py:8: error: "object" not callable  [operator]
Found 3 errors in 1 file (checked 1 source file)

This error only occurs in strict mode:

pydampy/config.py:6: error: Class cannot subclass "BaseSettings" (has type "Any")  [misc]

Example Code

I have a simple config class like this one:


from typing import Literal
from pydantic import SettingsConfigDict
from pydantic_settings import BaseSettings


class Config(BaseSettings):
    log_level: Literal["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "WARNING"
    model_config = SettingsConfigDict(env_file=".env")


This is my `mypy.ini`:

```ini
[mypy]
plugins = pydantic.mypy
strict = true

follow_imports = silent
warn_redundant_casts = True
warn_unused_ignores = True
disallow_any_generics = True
check_untyped_defs = True
no_implicit_reexport = True

# for strict mypy: (this is the tricky one :-))
disallow_untyped_defs = True

[pydantic-mypy]
init_forbid_extra = True
init_typed = True
warn_required_dynamic_aliases = True

Python, Pydantic & OS Version

pydantic version: 2.0.1
        pydantic-core version: 2.0.2 release build profile
                 install path: /home/user/git/minimals/pydantic2-mypy/venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.3 (main, Jun  5 2023, 09:32:32) [GCC 13.1.1 20230429]
                     platform: Linux-6.4.1-arch2-1-x86_64-with-glibc2.37
     optional deps. installed: ['typing-extensions']
$ pip list
Package           Version
----------------- -------
annotated-types   0.5.0
mypy              1.4.1
mypy-extensions   1.0.0
pip               22.3.1
pydantic          2.0.1
pydantic_core     2.0.2
setuptools        65.5.0
typing_extensions 4.7.1


Selected Assignee: @hramezani

Selected Assignee: @dmontagu
@hramezani
Copy link
Member

Thanks @sr-verde for reporting this issue 🙏

You've imported SettingsConfigDict from pydantic and I think this is wrong. you have to import it from pydantic_settings.

I've tested your provided example by changing the import and it looks fine:

from typing import Literal
from pydantic_settings import BaseSettings, SettingsConfigDict


class Config(BaseSettings):
    log_level: Literal["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "WARNING"
    model_config = SettingsConfigDict(env_file=".env")

Could you please try it?

@hramezani hramezani transferred this issue from pydantic/pydantic Jul 5, 2023
@sr-verde
Copy link
Author

sr-verde commented Jul 5, 2023

Yes, that solves the issue. And I know how it happened. Seems to be a bug in bump-pydantic.

I had this config in Pydantic v1:

from typing import Literal
from pydantic import BaseSettings


class Config(BaseSettings):
    log_level: Literal["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "WARNING"

    class Config:
        env_file = ".env"

Then, I executed dump-pydantic and got the following config file:

from typing import Literal
from pydantic import ConfigDict
from pydantic_settings import BaseSettings


class Config(BaseSettings):
    log_level: Literal["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "WARNING"
    model_config = ConfigDict(env_file=".env")

As one can see bump-pydantic imported ConfigDict from pydantic instead from pydantic_settings.

@hramezani
Copy link
Member

Thanks for the clarification.

It would be great to open an issue in bump-pydantic

@sr-verde
Copy link
Author

sr-verde commented Jul 5, 2023

Thanks for your help!

pydantic/bump-pydantic#52

@sr-verde
Copy link
Author

sr-verde commented Jul 5, 2023

Hey, sorry, need to reopen that issue again. Using strict mode in mypy this error still occurs:

pydampy/config.py:6: error: Class cannot subclass "BaseSettings" (has type "Any")  [misc]

@hramezani
Copy link
Member

Could you install pydantic-settings from the main branch and test it?

I couldn't see the error by the mypy config you provided.

@sr-verde
Copy link
Author

sr-verde commented Jul 5, 2023

Yes, seems to be fixed in main branch. So I’ll wait for the next release. Sorry to interrupt you again.

@hramezani
Copy link
Member

No worries. new release will come probably today.

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