diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index ef2d774..919f836 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -982,7 +982,7 @@ def __call__(self) -> dict[str, Any]: # As `extra` config is allowed in dotenv settings source, We have to # update data with extra env variables from dotenv file. for env_name, env_value in self.env_vars.items(): - if not env_value: + if not env_value or env_name in data: continue env_used = False for field_name, field in self.settings_cls.model_fields.items(): diff --git a/tests/test_settings.py b/tests/test_settings.py index c5d6406..f7ea7c2 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -2715,3 +2715,20 @@ class Settings(BaseSettings): s = Settings() assert s.model_dump() == {'not_nested': 'works', 'NESTED': {'A': 'fails', 'b': 2}} + + +def test_dotenv_env_prefix_env_without_prefix(tmp_path): + p = tmp_path / '.env' + p.write_text('test_foo=test-foo\nfoo=foo') + + class Settings(BaseSettings): + model_config = SettingsConfigDict( + env_file=p, + env_prefix='TEST_', + extra='ignore', + ) + + foo: str + + s = Settings() + assert s.model_dump() == {'foo': 'test-foo'}