Replies: 6 comments · 5 replies
-
|
I'll shamelessly self-bump this. Why I think being able to change |
Beta Was this translation helpful? Give feedback.
-
|
I would like to put some effort into implementing this myself, if this has some potential to be included in a future versions of pydantic. Any thoughts on this? |
Beta Was this translation helpful? Give feedback.
-
|
I'd love/need that as well. I tried everything but nothing I came up with worked? ` class Config: Setting.Config = Config |
Beta Was this translation helpful? Give feedback.
-
|
If anyone is also interested in this, I've found out that we need to do two things in order for it to work:
I've put together a hack that works by doing something like this (yes, I know it is fugly): import pydantic
class MySettings(pydantic.BaseSettings):
param_a: bool
runtime_env_prefix = "something_set_at_runtime"
# 1. override the original env_prefix
MySettings.__config__.env_prefix = runtime_env_prefix
# 2. prepare fields again with the new env_prefix in place
for field in MySettings.__fields__.values():
MySettings.__config__.prepare_field(field)
# now we can create our settings and it will look for env variables with the runtime env prefix
settings = MySettings() |
Beta Was this translation helpful? Give feedback.
-
|
Running pydantic-settings 2.0.0 - I use inheritance to do this. For me it works better, as I don't need to worry about which import pydantic_settings
class FooLikeSettings(pydantic_settings.BaseSettings):
model_config = (
pydantic_settings.SettingsConfigDict(
env_prefix="foo_",
)
... # model definition
class Bar(FooLikeSettings):
pass
Bar.model_config["env_prefix"] = "bar_" Not sure if this is the right thing. Probably this should be discouraged in favor of creating nested models. |
Beta Was this translation helpful? Give feedback.
-
|
I also have a need for this behavior. In the application I'm working on, users will need to configure connections to external data sources, many of which may be various instances of the same type of connection. Think of Because these connections will necessarily handle secrets, I would like to use Having reviewed the above discussion and related threads, I've also concluded that subclassing is probably the most stable way to implement this feature, but because users will not be interacting with the code directly (they will be using either a CLI or a web app backed by Pydantic models), the subclassing (if that is indeed the best solution) will need to be dynamic. Here's what I've come up with so far. Very interested to know what others think of this solution, perhaps particularly @dmontagu as IIUC you are the sole Pydantic core dev on this thread (apologies if I've misunderstood that). I'm particularly concerned with making sure my solution is stable, as this will eventually be deployed to a large user base. Thank you so much in advance for taking a look, and any feedback! # change-prefix.py
import sys
from typing import Type
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
key: str
secret: str
@classmethod
def dynamically_subclass_with_prefix(cls: Type["Settings"], env_prefix: str) -> "Settings":
"""Dynamically subclass the Settings class, using the user-supplied `env_prefix`."""
new_cls_name = env_prefix.title() + "Settings"
config = {"env_prefix": f"{env_prefix}_", "case_sensitive": False}
_cls = type(
new_cls_name,
(cls,),
{"model_config": SettingsConfigDict(**config)},
)
return _cls()
if __name__ == "__main__":
env_prefix = sys.argv[1]
settings = Settings.dynamically_subclass_with_prefix(env_prefix)
print(f"{settings = }")
print(f"{settings.key = }")
print(f"{settings.secret = }")➜ FIRST_KEY="abc" FIRST_SECRET="123" python3 change-prefix.py "FIRST"
settings = FirstSettings(key='abc', secret='123')
settings.key = 'abc'
settings.secret = '123'
➜ SECOND_KEY="def" SECOND_SECRET="456" python3 change-prefix.py "SECOND"
settings = SecondSettings(key='def', secret='456')
settings.key = 'def'
settings.secret = '456'
➜ ANOTHER_KEY="ghi" ANOTHER_SECRET="789" python3 change-prefix.py "ANOTHER"
settings = AnotherSettings(key='ghi', secret='789')
settings.key = 'ghi'
settings.secret = '789' |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, is it possible to change
env_prefixat runtime? I've tried some hacks which didn't work (playing with inheritance, overriding) but noting worked. Something like this would be nice:Beta Was this translation helpful? Give feedback.
All reactions