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
- pydantic plugin 으로 false negative alert 해결 (https://github.com/pydantic/pydantic/issues/3072), __init__ 을 마이그레이션 폴더에 넣어줌으로서 모듈로 인식되도록 변경. — committed to deokbaae/oz_fastapi by triumph1 4 months ago
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.
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)Also using plugins = [“pydantic.mypy”] in pyproject.toml
Thanks for your help
I can confirm this with:
1.10.5
1.0.1
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.
I agree with @jc-rosier I still get these errors. This issue should be reopened.