@@ -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,126 @@ 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_settings import (
2450+ BaseSettings,
2451+ PydanticBaseSettingsSource,
2452+ SettingsConfigDict,
2453+ TomlConfigSettingsSource,
2454+ )
2455+
2456+ class Nested (BaseModel ):
2457+ foo: int
2458+ bar: int = 0
2459+
2460+
2461+ class Settings (BaseSettings ):
2462+ hello: str
2463+ nested: Nested
2464+ model_config = SettingsConfigDict(toml_file = [' config.default.toml' , ' config.custom.toml' ])
2465+
2466+ @ classmethod
2467+ def settings_customise_sources (
2468+ cls ,
2469+ settings_cls : type[BaseSettings],
2470+ init_settings : PydanticBaseSettingsSource,
2471+ env_settings : PydanticBaseSettingsSource,
2472+ dotenv_settings : PydanticBaseSettingsSource,
2473+ file_secret_settings : PydanticBaseSettingsSource,
2474+ ) -> tuple[PydanticBaseSettingsSource, ... ]:
2475+ return (TomlConfigSettingsSource(settings_cls),)
2476+ ```
2477+
2478+ The following two configuration files
2479+
2480+ ``` toml
2481+ # config.default.toml
2482+ hello = " World"
2483+
2484+ [nested ]
2485+ foo = 1
2486+ bar = 2
2487+ ```
2488+
2489+ ``` toml
2490+ # config.custom.toml
2491+ [nested ]
2492+ foo = 3
2493+ ```
2494+
2495+ are equivalent to
2496+
2497+ ``` toml
2498+ hello = " world"
2499+
2500+ [nested ]
2501+ foo = 3
2502+ ```
2503+
2504+ The files are merged shallowly in increasing order of priority. To enable deep merging, set ` deep_merge=True ` on the source directly.
2505+
2506+ !!! warning
2507+ The ` deep_merge ` option is ** not available** through the ` SettingsConfigDict ` .
2508+
2509+ ``` py
2510+ from pydantic_settings import (
2511+ BaseSettings,
2512+ PydanticBaseSettingsSource,
2513+ SettingsConfigDict,
2514+ TomlConfigSettingsSource,
2515+ )
2516+
2517+ class Nested (BaseModel ):
2518+ foo: int
2519+ bar: int = 0
2520+
2521+
2522+ class Settings (BaseSettings ):
2523+ hello: str
2524+ nested: Nested
2525+ model_config = SettingsConfigDict(toml_file = [' config.default.toml' , ' config.custom.toml' ])
2526+
2527+ @ classmethod
2528+ def settings_customise_sources (
2529+ cls ,
2530+ settings_cls : type[BaseSettings],
2531+ init_settings : PydanticBaseSettingsSource,
2532+ env_settings : PydanticBaseSettingsSource,
2533+ dotenv_settings : PydanticBaseSettingsSource,
2534+ file_secret_settings : PydanticBaseSettingsSource,
2535+ ) -> tuple[PydanticBaseSettingsSource, ... ]:
2536+ return (TomlConfigSettingsSource(settings_cls, deep_merge = True ),)
2537+ ```
2538+
2539+ With deep merge enabled, the following two configuration files
2540+
2541+ ``` toml
2542+ # config.default.toml
2543+ hello = " World"
2544+
2545+ [nested ]
2546+ foo = 1
2547+ bar = 2
2548+ ```
2549+
2550+ ``` toml
2551+ # config.custom.toml
2552+ [nested ]
2553+ foo = 3
2554+ ```
2555+
2556+ are equivalent to
2557+
2558+ ``` toml
2559+ hello = " world"
2560+
2561+ [nested ]
2562+ foo = 3
2563+ bar = 2
2564+ ```
2565+
24512566### pyproject.toml
24522567
24532568"pyproject.toml" is a standardized file for providing configuration values in Python projects.
0 commit comments