I'm using Pydantic settings management in a FastAPI-based project. I have a Settings
class like this one:
class Settings(BaseSettings):
FOO: str = ''
BAR: int = 0
class Config:
env_file = "path/to/.my_env_file")
env_nested_delimiter = "__"
The file path/to/.my_env_file
contains FOO
and BAR
values.
During tests, I need to selectively patch Settings
, and I do not want to read anything from path/to/.my_env_file
. E.g.,
path/to/.my_env_file
FOO=i_do_not_wanna_read_this
BAR=100
my test file:
@lru_cache()
def get_settings():
return Settings()
def get_settings_override() -> Settings:
return Settings(
FOO = 'foo'
)
app.dependency_overrides[get_settings] = get_settings_override
I want to run tests with FOO='foo'
and with the default value of BAR (i.e., BAR=0
, ignoring the content of path/to/.my_env_file
. In the code above, I get FOO='foo'
but BAR
is still read from path/to/.my_env_file
(i.e., BAR=100
)
Is there a straightforward way to handle that?