Skip to content

Error when a submodule has not values in the .env file #645

Closed
@GiamBoscaro

Description

@GiamBoscaro

Take this configuration as an example:

class A(BaseSettings):    
    FOO: str = "afoo"
    BAR: str = "abar"
    
class B(BaseSettings):    
    FOO: str = "bfoo"
    BAR: str = "bbar"

class Settings(BaseSettings, case_sensitive=True, env_file=".env", env_nested_delimiter="_", env_nested_max_split=1):
    A: A
    B: B    
    APP_NAME: str = "my-app"
    PY_ENV: str

and this env file:

A_FOO="something"
A_BAR="something else"

This will throw the error

pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
B

I need to insert in the .env file at least one variable that belong to the B model, then everything works and the variables are initiated in the B model, the missing ones with the default values.

A_FOO="something"
A_BAR="something else"
B_FOO="default"

Now, what if I really just need the default values for the whole B model 90% of the times. I would like to avoid putting variables in the .env file that are exactly the default values just to make it work.

How can I be sure that the .env submodule is correctly instantiated with all the default values even if in the .env file there are not B_ variables?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions