Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apparent breaking change in DotEnvSettingsSource when extra="allow" #237

Closed
zzstoatzz opened this issue Feb 19, 2024 · 0 comments · Fixed by #238
Closed

apparent breaking change in DotEnvSettingsSource when extra="allow" #237

zzstoatzz opened this issue Feb 19, 2024 · 0 comments · Fixed by #238
Assignees

Comments

@zzstoatzz
Copy link
Contributor

zzstoatzz commented Feb 19, 2024

a breaking change to how DotEnvSettingsSource handles setting values from an .env file when the settings config has extra='allow' seems to have been introduced with 2.2.0 via #221

In particular, it seems that env_prefix now remains present on the "extra" setting attribute name instead of being removed as it was before 2.2.0

I will take a stab at a fix

MRE
» tree -a
.
├── .env
└── settings.py

settings.py

from pydantic_settings import BaseSettings, SettingsConfigDict

class FooSettings(BaseSettings):
    model_config = SettingsConfigDict(
        extra="allow",
        env_file=".env",
        env_prefix="foo_"
    )
    
    x: str

settings = FooSettings()

print(f'{getattr(settings, "y", None)=}')
print(f'{getattr(settings, "foo_y", None)=}')

.env

FOO_X=something
FOO_Y=something_else

on 2.2.0

(pydantic-settings-2dot2) pad-2 :: src/open-source/mre
» python settings.py
getattr(settings, "y", None)=None
getattr(settings, "foo_y", None)='something_else'

» pip list | rg settings
pydantic-settings 2.2.0

on 2.1.0

(pydantic-settings-2dot1) pad-2 :: src/open-source/mre
» python settings.py
getattr(settings, "y", None)='something_else'
getattr(settings, "foo_y", None)=None

» pip list | rg settings
pydantic-settings 2.1.0
@zzstoatzz zzstoatzz changed the title apparent breaking change for BaseSettings with extra="allow" apparent breaking change in DotEnvSettingsSource when extra="allow" Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants