pydantic / pydantic-settings

Settings management using pydantic

Home Page:https://docs.pydantic.dev/latest/usage/pydantic_settings/

Repository from Github https://github.compydantic/pydantic-settingsRepository from Github https://github.compydantic/pydantic-settings

CLI: creating an optional nested model with all default values

rredburn opened this issue · comments

I've been trying (and failing) to find a way to allow nested models with default values for every field to be optional by default or signal creation of a default instance. For example, given:

class Nested(BaseModel):
    foo: int = 1
    bar: int = 2

class Options(BaseSettings):
    nested: Optional[Nested] = None

I'd like to enable three invocations:

$ options   # nested is None, app ignores it

$ options --nested   # create a fully default nested

$ options --nested.foo 3   # create modified nested

It appears the best I can do is require someone pass at least one default value directly or allow json and pass an empty dict:

$ options --nested.foo 1   # same as default, works but redundant, error prone

$ options --nested {}   # works but not intuitive for users.  can the braces be eliminated?

Is there a way to do this I'm overlooking or could it be a possible enhancement?
Thanks.

@kschwab could you please take a look?

@rredburn this is a nice idea:

$ options --nested # create a fully default nested

Will take a look and see how difficult it is to add. I also suspect we might have to watch out for backwards compatibility.

@hramezani @rredburn opened #581 for resolution.