Skip to content

Commit a72fa73

Browse files
authored
Fix bug in dotenv source when there is env with and without prefix (#440)
1 parent d2e498a commit a72fa73

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pydantic_settings/sources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ def __call__(self) -> dict[str, Any]:
982982
# As `extra` config is allowed in dotenv settings source, We have to
983983
# update data with extra env variables from dotenv file.
984984
for env_name, env_value in self.env_vars.items():
985-
if not env_value:
985+
if not env_value or env_name in data:
986986
continue
987987
env_used = False
988988
for field_name, field in self.settings_cls.model_fields.items():

tests/test_settings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,3 +2715,20 @@ class Settings(BaseSettings):
27152715

27162716
s = Settings()
27172717
assert s.model_dump() == {'not_nested': 'works', 'NESTED': {'A': 'fails', 'b': 2}}
2718+
2719+
2720+
def test_dotenv_env_prefix_env_without_prefix(tmp_path):
2721+
p = tmp_path / '.env'
2722+
p.write_text('test_foo=test-foo\nfoo=foo')
2723+
2724+
class Settings(BaseSettings):
2725+
model_config = SettingsConfigDict(
2726+
env_file=p,
2727+
env_prefix='TEST_',
2728+
extra='ignore',
2729+
)
2730+
2731+
foo: str
2732+
2733+
s = Settings()
2734+
assert s.model_dump() == {'foo': 'test-foo'}

0 commit comments

Comments
 (0)