-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Use config values as default in PydanticBaseEnvSettingsSource.__init__
#100
Conversation
please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I don't think this addresses the issue unfortunately. Here's a MRE of what should be expected:
class CustomEnvSettingsSource(EnvSettingsSource):
pass
class Settings(BaseSettings):
foo: str
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource
) -> tuple[PydanticBaseSettingsSource, ...]:
custom_env_settings = CustomEnvSettingsSource(settings_cls) # CustomEnvSettingsSource should be able to use config values either from `model_config` or values given at instantiation
return init_settings, custom_env_settings, dotenv_settings, file_secret_settings
os.environ["prefix_foo"] = "test_foo"
s = Settings(_env_prefix="prefix_")
assert s.foo == "test_foo"
What the added tests show is that CustomEnvSettingsSource
is able to read values from model_config
, but not from instantation arguments 😕
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #100 +/- ##
=======================================
Coverage 97.42% 97.42%
=======================================
Files 5 5
Lines 311 311
Branches 68 68
=======================================
Hits 303 303
Misses 6 6
Partials 2 2
☔ View full report in Codecov by Sentry. |
You can pass custom_env_settings = CustomEnvSettingsSource(settings_cls, env_prefix="prefix_") or define it in config as I shared an example in the issue |
But the I understand the MRE I gave here can't be supported, as trying to support it might lead to too many breaking changes. |
@Viicos If you define a custom source, you need to initialize it in
I would be happy to take a look at that. we can merge it if it is an easy one. because as I mentioned above, you can initialize a custom source in |
I merged without seeing the messages above, sorry. Hope is cool. |
Yes this is still an improvement, thanks for merging |
Fixes #98
Selected Reviewer: @samuelcolvin