pydantic: mypy error when using `_env_file` in BaseSettings sub-class

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn’t find anything
  • I have read and followed the docs and still think this is a bug

Bug

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

             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: /Users/jduckworth/.pyenv/versions/3.8.9/envs/mlhub-backend-dev/lib/python3.8/site-packages/pydantic
               python version: 3.8.9 (default, Apr 23 2021, 14:38:08)  [Clang 12.0.0 (clang-1200.0.32.28)]
                     platform: macOS-10.15.7-x86_64-i386-64bit
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']

Running mypy --show-error-codes ./pydantic_mypy_error.py with the files shown below gives the following error:

pydantic_mypy_error.py:9: error: Unexpected keyword argument "_env_file" for "EnvFileBaseSettings"  [call-arg]
Found 1 error in 1 file (checked 1 source file)

mypy.ini

[mypy]
plugins = pydantic.mypy

pydantic_mypy_error.py


from pydantic import BaseSettings


class EnvFileBaseSettings(BaseSettings):
    my_var: str


if __name__ == "__main__":
    _ = EnvFileBaseSettings(_env_file=".test.env")

.test.env

# .test.env
MY_VAR=thing

Selected Assignee: @davidhewitt

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 17
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

We are not talking about the same thing here, please stop confusing the thread.

https://docs.pydantic.dev/latest/usage/pydantic_settings/#dotenv-env-support

As in the official documentation of pydantic settings, I want to use the _env_file keyword argument to override the env file location at runtime.

Instantiating the BaseSettings derived class with the _env_file keyword argument (and the _env_file_encoding if needed):

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')


settings = Settings(_env_file='prod.env', _env_file_encoding='utf-8')

error: Unexpected keyword argument "_env_file" for "Settings" [call-arg]

Hello, even with python 3.11, pydantic-settings 2.0.2 and mypy 1.4.1 I got the error:

error: Unexpected keyword argument "_env_file" for "Settings" [call-arg] Found 1 error in 1 file (checked 1 source file)

from pydantic_settings import BaseSettings

class Settings(BaseSettings)
    pass

Settings(_env_file="")

Also using plugins = [“pydantic.mypy”] in pyproject.toml

Thanks for your help

I can confirm this with:

  • Pydantic version 1.10.5
  • mypy version 1.0.1
  • with
    [tool.mypy]
    plugins = [ "pydantic.mypy" ]
    
    in pyproject.toml

I have the same error with _env_file and with _env_file_encoding too.

Using SettingsConfigDict I am having no issues. You can try using that syntax.

I believe the syntax you’re using is officially documented as well, though passing arguments starting with underscores is a big hint that it might not be the real intended way to do things.

from pydantic_settings import BaseSettings
from pydantic_settings import SettingsConfigDict

class Settings(BaseSettings)
    
    model_config = SettingsConfigDict(env_file=".env")

Settings()

I agree with @jc-rosier I still get these errors. This issue should be reopened.

mypy                      1.4.1
pydantic                  2.0.2
pydantic_core             2.1.2
pydantic-settings         2.0.1
[tool.mypy]
ignore_missing_imports = true
strict = true
disallow_untyped_decorators = false

plugins = [
    "pydantic.mypy",
]

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true