Closed mrijken closed 1 month ago
Thanks @mrijken for reporting this.
Actually, this is the correct behavior. you have db1: DatabaseSettings = DatabaseSettings(name="database1")
in your model. it means the default value for db1
is DatabaseSettings(name="database1", env="test")
. when you pass {"env": "prd"}
, it will override the default with DatabaseSettings(name='test', env='prd')
Hi @mrijken, I've added a flag nested_model_default_partial_update
that enables partial updates for default model objects. Could you help verify it resolves your issue and post a simple test for your use case? I believe the case posted in the description is now resolved:
from pydantic import BaseModel
from pydantic_settings import BaseSettings
class DatabaseSettings(BaseModel):
name: str = 'test'
env: str = 'test'
class Settings(BaseSettings, nested_model_default_partial_update=True):
db1: DatabaseSettings = DatabaseSettings(name='database1')
db2: DatabaseSettings = DatabaseSettings(name='database2')
print(Settings(db1={'env': 'prd'}))
#> db1=DatabaseSettings(name='database1', env='prd') db2=DatabaseSettings(name='database2', env='test')
When I have the next Models:
I will get:
However, I would expect:
I have written a small PydanticBaseSettingsSource fixes this behavior:
Is the issue the expected behavior? Is the proposed solution the correct one? Shall I make a PR?