@@ -2401,12 +2401,7 @@ Other settings sources are available for common configuration files:
24012401- ` TomlConfigSettingsSource ` using ` toml_file ` argument
24022402- ` YamlConfigSettingsSource ` using ` yaml_file ` and yaml_file_encoding arguments
24032403
2404- You can also provide multiple files by providing a list of path:
2405- ``` py
2406- toml_file = [' config.default.toml' , ' config.custom.toml' ]
2407- ```
2408- To use them, you can use the same mechanism described [ here] ( #customise-settings-sources )
2409-
2404+ To use them, you can use the same mechanism described [ here] ( #customise-settings-sources ) .
24102405
24112406``` py
24122407from pydantic import BaseModel
@@ -2448,6 +2443,136 @@ foobar = "Hello"
24482443nested_field = " world!"
24492444```
24502445
2446+ You can also provide multiple files by providing a list of paths.
2447+
2448+ ``` py
2449+ from pydantic import BaseModel
2450+
2451+ from pydantic_settings import (
2452+ BaseSettings,
2453+ PydanticBaseSettingsSource,
2454+ SettingsConfigDict,
2455+ TomlConfigSettingsSource,
2456+ )
2457+
2458+
2459+ class Nested (BaseModel ):
2460+ foo: int
2461+ bar: int = 0
2462+
2463+
2464+ class Settings (BaseSettings ):
2465+ hello: str
2466+ nested: Nested
2467+ model_config = SettingsConfigDict(
2468+ toml_file = [' config.default.toml' , ' config.custom.toml' ]
2469+ )
2470+
2471+ @ classmethod
2472+ def settings_customise_sources (
2473+ cls ,
2474+ settings_cls : type[BaseSettings],
2475+ init_settings : PydanticBaseSettingsSource,
2476+ env_settings : PydanticBaseSettingsSource,
2477+ dotenv_settings : PydanticBaseSettingsSource,
2478+ file_secret_settings : PydanticBaseSettingsSource,
2479+ ) -> tuple[PydanticBaseSettingsSource, ... ]:
2480+ return (TomlConfigSettingsSource(settings_cls),)
2481+ ```
2482+
2483+ The following two configuration files
2484+
2485+ ``` toml
2486+ # config.default.toml
2487+ hello = " World"
2488+
2489+ [nested ]
2490+ foo = 1
2491+ bar = 2
2492+ ```
2493+
2494+ ``` toml
2495+ # config.custom.toml
2496+ [nested ]
2497+ foo = 3
2498+ ```
2499+
2500+ are equivalent to
2501+
2502+ ``` toml
2503+ hello = " world"
2504+
2505+ [nested ]
2506+ foo = 3
2507+ ```
2508+
2509+ The files are merged shallowly in increasing order of priority. To enable deep merging, set ` deep_merge=True ` on the source directly.
2510+
2511+ !!! warning
2512+ The ` deep_merge ` option is ** not available** through the ` SettingsConfigDict ` .
2513+
2514+ ``` py
2515+ from pydantic import BaseModel
2516+
2517+ from pydantic_settings import (
2518+ BaseSettings,
2519+ PydanticBaseSettingsSource,
2520+ SettingsConfigDict,
2521+ TomlConfigSettingsSource,
2522+ )
2523+
2524+
2525+ class Nested (BaseModel ):
2526+ foo: int
2527+ bar: int = 0
2528+
2529+
2530+ class Settings (BaseSettings ):
2531+ hello: str
2532+ nested: Nested
2533+ model_config = SettingsConfigDict(
2534+ toml_file = [' config.default.toml' , ' config.custom.toml' ]
2535+ )
2536+
2537+ @ classmethod
2538+ def settings_customise_sources (
2539+ cls ,
2540+ settings_cls : type[BaseSettings],
2541+ init_settings : PydanticBaseSettingsSource,
2542+ env_settings : PydanticBaseSettingsSource,
2543+ dotenv_settings : PydanticBaseSettingsSource,
2544+ file_secret_settings : PydanticBaseSettingsSource,
2545+ ) -> tuple[PydanticBaseSettingsSource, ... ]:
2546+ return (TomlConfigSettingsSource(settings_cls, deep_merge = True ),)
2547+ ```
2548+
2549+ With deep merge enabled, the following two configuration files
2550+
2551+ ``` toml
2552+ # config.default.toml
2553+ hello = " World"
2554+
2555+ [nested ]
2556+ foo = 1
2557+ bar = 2
2558+ ```
2559+
2560+ ``` toml
2561+ # config.custom.toml
2562+ [nested ]
2563+ foo = 3
2564+ ```
2565+
2566+ are equivalent to
2567+
2568+ ``` toml
2569+ hello = " world"
2570+
2571+ [nested ]
2572+ foo = 3
2573+ bar = 2
2574+ ```
2575+
24512576### pyproject.toml
24522577
24532578"pyproject.toml" is a standardized file for providing configuration values in Python projects.
0 commit comments