From 0d605d076553452f67141f00e5dc873aa06c5b01 Mon Sep 17 00:00:00 2001 From: Kyle Schwab Date: Wed, 2 Oct 2024 03:46:30 -0600 Subject: [PATCH] Update nested model partial update docs example. (#433) --- docs/index.md | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/index.md b/docs/index.md index 3e846c9..cc58bd2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -390,18 +390,36 @@ class SubModel(BaseModel): flag: bool = False -class Settings(BaseSettings): +class SettingsPartialUpdate(BaseSettings): model_config = SettingsConfigDict( env_nested_delimiter='__', nested_model_default_partial_update=True ) - nested_model: SubModel = SubModel() + nested_model: SubModel = SubModel(val=1) + + +class SettingsNoPartialUpdate(BaseSettings): + model_config = SettingsConfigDict( + env_nested_delimiter='__', nested_model_default_partial_update=False + ) + + nested_model: SubModel = SubModel(val=1) # Apply a partial update to the default object using environment variables os.environ['NESTED_MODEL__FLAG'] = 'True' -assert Settings().model_dump() == {'nested_model': {'val': 0, 'flag': True}} +# When partial update is enabled, the existing SubModel instance is updated +# with nested_model.flag=True change +assert SettingsPartialUpdate().model_dump() == { + 'nested_model': {'val': 1, 'flag': True} +} + +# When partial update is disabled, a new SubModel instance is instantiated +# with nested_model.flag=True change +assert SettingsNoPartialUpdate().model_dump() == { + 'nested_model': {'val': 0, 'flag': True} +} ``` ## Dotenv (.env) support