-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
Ensure extra='forbid' is enforced in DotEnvSettingsSource when env_prefix is specified #218
Ensure extra='forbid' is enforced in DotEnvSettingsSource when env_prefix is specified #218
Conversation
…Source when env_prefix is specified The current implementation in DotEnvSettingsSource allows non-prefixed variables even when extra='forbid'(default) is set . This commit addresses the issue by raising a SettingsError when extra is set to 'forbid' and variables are encountered without using the specified prefix.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #218 +/- ##
==========================================
+ Coverage 97.61% 97.64% +0.02%
==========================================
Files 5 5
Lines 336 339 +3
Branches 71 72 +1
==========================================
+ Hits 328 331 +3
Misses 6 6
Partials 2 2 ☔ View full report in Codecov by Sentry. |
please review |
Thanks @kjithin for working on this 🙏 I think raising e.g. if we have a settings model like: from pydantic_settings import BaseSettings, SettingsConfigDict
class Foo(BaseSettings):
a: str
model_config = SettingsConfigDict(env_file="my.env") with
we will get
So, we want to have the same behavior in the case of the user providing |
please update |
@hramezani . Thanks for the feedback. Initially, I explored that option. However, when collecting all the environment variables, a challenge arose: it would accept a valid property without a prefix. This occurred because the expected output for this function is a dictionary without prefixes in the keys. for e.g. from pydantic_settings import BaseSettings, SettingsConfigDict
class Foo(BaseSettings):
a: str
b: str
model_config = SettingsConfigDict(env_file="my.env", env_prefix="prefix_") with
The identical ValidationError will only be triggered during the final model validation against the generated dictionary, specifically if it includes any additional properties. In the above case, when collecting all the env variables, both |
Good point @kjithin |
Fix #215
The current implementation in DotEnvSettingsSource allows non-prefixed variables even when extra='forbid'(default) is set . This commit addresses the issue by raising a SettingsError when extra is set to 'forbid' and variables are encountered without using the specified prefix.
Selected Reviewer: @dmontagu