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

"Config" overrides symbol of same name in class "BaseSettings" #4223

Closed
adithep opened this issue Jul 9, 2022 · 7 comments
Closed

"Config" overrides symbol of same name in class "BaseSettings" #4223

adithep opened this issue Jul 9, 2022 · 7 comments
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@adithep
Copy link

adithep commented Jul 9, 2022

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

 pydantic version: 1.9.1
            pydantic compiled: True
                 install path: /Users/adithepsrinarula/repo/prometheus-hoarding/dist/export/python/virtualenv/3.10.5/lib/python3.10/site-packages/pydantic
               python version: 3.10.5 (main, Jul  4 2022, 16:56:19) [Clang 13.1.6 (clang-1316.0.21.2.5)]
                     platform: macOS-12.4-arm64-arm-64bit
     optional deps. installed: ['dotenv', 'typing-extensions']

import pydantic

class Settings(pydantic.BaseSettings):

    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"

I get error:

Screen Shot 2565-07-09 at 18 33 49

@adithep adithep added the bug V1 Bug related to Pydantic V1.X label Jul 9, 2022
@RobertCraigie
Copy link

@adithep Pylance is correct here. You have to explicitly subclass the Config, e.g.

import pydantic

class Settings(pydantic.BaseSettings):
    class Config(pydantic.BaseSettings.Config):
        env_file = '.env'
        env_file_encoding = 'utf-8'

@adithep adithep closed this as completed Jul 12, 2022
@jannisko
Copy link

jannisko commented Aug 4, 2022

Should this maybe be changed in the docs as well? The example given here still causes an error in type checkers.

@DbCrWk
Copy link

DbCrWk commented Aug 29, 2022

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.9.2
            pydantic compiled: True
                 install path: /Users/dev.dabke/Documents/repo/ninja/nibbler/.venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.4 (main, Jul 18 2022, 16:17:04) [Clang 13.1.6 (clang-1316.0.21.2.5)]
                     platform: macOS-12.5.1-arm64-arm-64bit
     optional deps. installed: ['devtools', 'dotenv', 'typing-extensions']

For what it's worth, running

import pydantic

class Settings(pydantic.BaseSettings):
    class Config(pydantic.BaseSettings.Config):
        env_file = '.env'
        env_file_encoding = 'utf-8'

causes mypy to report:

Incompatible types in assignment (expression has type "str", base class "Config" defined the type as "None")

for the assignment env_file = '.env' and env_file_encoding = 'utf-8'.

I am running mypy version: mypy 0.971 (compiled: yes) and my pyproject.toml has this:

[tool.mypy]
plugins = [
    "pydantic.mypy"
]

Granted, the docs say that this plugin only works with mypy version 0.910, 0.920, 0.921 and 0.930, so maybe that is the issue?

CC: @RobertCraigie

@samuelcolvin
Copy link
Member

samuelcolvin commented Aug 30, 2022

pydantic should be compatible with all recent versions of mypy. This error is due to missing typing information on pydantic.BaseSettings.Config. Probably too late to fix this in V1.10, but we'll fix it in V2.

@samuelcolvin
Copy link
Member

Ignore the above, @DbCrWk I've fixed your issue in #4450

@DbCrWk
Copy link

DbCrWk commented Aug 30, 2022

Ignore the above, @DbCrWk I've fixed your issue in #4450

Wow, thanks for fixing this so quickly!

@gitpushdashf
Copy link

gitpushdashf commented Jan 27, 2023

Thank you! This is great.

However, I am still seeing class Config: instead of class Config(BaseSettings.Config): in the documentation: https://docs.pydantic.dev/usage/settings/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

6 participants