Skip to content
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

Populating embedded models with environment variables works but throws error with env file #172

Closed
robcxyz opened this issue Sep 29, 2023 · 2 comments
Assignees

Comments

@robcxyz
Copy link

robcxyz commented Sep 29, 2023

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:

import os
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field, ValidationError


class Child(BaseSettings):
    foo: str | None = None
    model_config = SettingsConfigDict(
        env_prefix='parent_child_',
        case_sensitive=False,
    )


class Parent(BaseSettings):
    child: Child = Field(default_factory=Child)
    model_config = SettingsConfigDict(
        env_prefix='parent_',
        case_sensitive=False,
    )

# Instantiate normally
p = Parent()
assert p.child.foo is None

# Instantiate with env var
os.environ['PARENT_CHILD_FOO'] = 'bar'
p = Parent()
assert p.child.foo == 'bar'
os.environ.pop('PARENT_CHILD_FOO')

# Instantiate with env file
env_file_contents = """
PARENT_CHILD_FOO=baz
"""
env_file_name = 'new_env_file'
with open(env_file_name, 'w') as f:
    f.write(env_file_contents)
try:
    p = Parent(_env_file=env_file_name)
except ValidationError as e:
    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.
@hramezani
Copy link
Member

hramezani commented Oct 2, 2023

Thanks @robcxyz for reporting this issue 🙏

There are couple of problem to fix:

1- As you can see in the docs, sub model has to inherit from pydantic.BaseModel

2- You need to specify env_nested_delimiter config

I've updated your provided example with the changes:

import os
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import BaseModel, Field, ValidationError


class Child(BaseModel):  # change
    foo: str | None = None


class Parent(BaseSettings):
    child: Child = Field(default_factory=Child)
    model_config = SettingsConfigDict(
        env_prefix='parent_',
        case_sensitive=False,
        env_nested_delimiter='__',  # change
    )

# Instantiate normally
p = Parent()
assert p.child.foo is None

# Instantiate with env var
os.environ['PARENT_CHILD__FOO'] = 'bar'  # change
p = Parent()
assert p.child.foo == 'bar'
os.environ.pop('PARENT_CHILD__FOO')  # change

# Instantiate with env file
env_file_contents = """
PARENT_CHILD__FOO=baz  # change
"""
env_file_name = 'new_env_file'
with open(env_file_name, 'w') as f:
    f.write(env_file_contents)
try:
    p = Parent(_env_file=env_file_name)
    assert p.child.foo == 'baz'  # change
except ValidationError as e:
    print(e)
    print('This should be valid given the environment variable was able to validate.')

os.remove(env_file_name)

@robcxyz
Copy link
Author

robcxyz commented Oct 3, 2023

@hramezani - Thank you so much for your response. This is exactly what I was looking for. Closing issue. Thanks again.

@robcxyz robcxyz closed this as completed Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants