You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When embedding two BaseSettings, I am able to populate the models with an environment variable but not with an env file. I would have expected the two methods to be equivalent but they are not.
Example:
importosfrompydantic_settingsimportBaseSettings, SettingsConfigDictfrompydanticimportField, ValidationErrorclassChild(BaseSettings):
foo: str|None=Nonemodel_config=SettingsConfigDict(
env_prefix='parent_child_',
case_sensitive=False,
)
classParent(BaseSettings):
child: Child=Field(default_factory=Child)
model_config=SettingsConfigDict(
env_prefix='parent_',
case_sensitive=False,
)
# Instantiate normallyp=Parent()
assertp.child.fooisNone# Instantiate with env varos.environ['PARENT_CHILD_FOO'] ='bar'p=Parent()
assertp.child.foo=='bar'os.environ.pop('PARENT_CHILD_FOO')
# Instantiate with env fileenv_file_contents="""PARENT_CHILD_FOO=baz"""env_file_name='new_env_file'withopen(env_file_name, 'w') asf:
f.write(env_file_contents)
try:
p=Parent(_env_file=env_file_name)
exceptValidationErrorase:
print(e)
print('This should be valid given the environment variable was able to validate.')
os.remove(env_file_name)
Yields
1 validation error for Parent
child_foo
Extra inputs are not permitted [type=extra_forbidden, input_value='baz', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/extra_forbidden
This should be valid given the environment variable was able to validate.
The text was updated successfully, but these errors were encountered:
When embedding two BaseSettings, I am able to populate the models with an environment variable but not with an env file. I would have expected the two methods to be equivalent but they are not.
Example:
Yields
The text was updated successfully, but these errors were encountered: