-
-
Notifications
You must be signed in to change notification settings - Fork 105
Open
Labels
enhancementNew feature or requestNew feature or request
Description
I expect a field value given while creating a model instance takes precedence over an existing environment variable.
It seems this is not possible here:
from pydantic import AnyHttpUrl, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
abc: AnyHttpUrl = Field(validation_alias="my_abc")
model_config = SettingsConfigDict(populate_by_name=True, extra="allow")
def test_(monkeypatch):
monkeypatch.setenv("MY_ABC", "http://localhost.com/")
assert (
str(Settings(abc="http://prod.localhost.com/").abc)
== "http://prod.localhost.com/"
)
One workaround is to use the validation alias when creating the instance:
from pydantic import AnyHttpUrl, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
abc: AnyHttpUrl = Field(validation_alias="my_abc")
model_config = SettingsConfigDict(populate_by_name=True, extra="allow")
def test_(monkeypatch):
monkeypatch.setenv("MY_ABC", "http://localhost.com/")
assert (
str(Settings(my_abc="http://prod.localhost.com/").abc)
== "http://prod.localhost.com/"
)
vdusek
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request