You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Other settings sources are available for common configuration files:
541
541
542
+
-`JsonConfigSettingsSource` using `json_file` and `json_file_encoding` arguments
543
+
-`PyprojectTomlConfigSettingsSource` using *(optional)*`pyproject_toml_depth` and *(optional)*`pyproject_toml_table_header` arguments
542
544
-`TomlConfigSettingsSource` using `toml_file` argument
543
545
-`YamlConfigSettingsSource` using `yaml_file` and yaml_file_encoding arguments
544
-
-`JsonConfigSettingsSource` using `json_file` and `json_file_encoding` arguments
545
546
546
547
You can also provide multiple files by providing a list of path:
547
548
```py
@@ -592,6 +593,127 @@ foobar = "Hello"
592
593
nested_field = "world!"
593
594
```
594
595
596
+
### pyproject.toml
597
+
598
+
"pyproject.toml" is a standardized file for providing configuration values in Python projects.
599
+
[PEP 518](https://peps.python.org/pep-0518/#tool-table) defines a `[tool]` table that can be used to provide arbitrary tool configuration.
600
+
While encouraged to use the `[tool]` table, `PyprojectTomlConfigSettingsSource` can be used to load variables from any location with in "pyproject.toml" file.
601
+
602
+
This is controlled by providing `SettingsConfigDict(pyproject_toml_table_header=tuple[str, ...])` where the value is a tuple of header parts.
603
+
By default, `pyproject_toml_table_header=('tool', 'pydantic-settings')` which will load variables from the `[tool.pydantic-settings]` table.
604
+
605
+
```python
606
+
from typing import Tuple, Type
607
+
608
+
from pydantic_settings import (
609
+
BaseSettings,
610
+
PydanticBaseSettingsSource,
611
+
PyprojectTomlConfigSettingsSource,
612
+
SettingsConfigDict,
613
+
)
614
+
615
+
616
+
classSettings(BaseSettings):
617
+
"""Example loading values from the table used by default."""
This will be able to read the following "pyproject.toml" file, located in your working directory, resulting in `Settings(field='default-table')`, `SomeTableSettings(field='some-table')`, & `RootSettings(field='root')`:
648
+
649
+
```toml
650
+
field = "root"
651
+
652
+
[tool.pydantic-settings]
653
+
field = "default-table"
654
+
655
+
[tool.some-table]
656
+
field = "some-table"
657
+
```
658
+
659
+
By default, `PyprojectTomlConfigSettingsSource` will only look for a "pyproject.toml" in the your current working directory.
660
+
However, there are two options to change this behavior.
661
+
662
+
*`SettingsConfigDict(pyproject_toml_depth=<int>)` can be provided to check `<int>` number of directories **up** in the directory tree for a "pyproject.toml" if one is not found in the current working directory.
663
+
By default, no parent directories are checked.
664
+
* An explicit file path can be provided to the source when it is instantiated (e.g. `PyprojectTomlConfigSettingsSource(settings_cls, Path('~/.config').resolve() / 'pyproject.toml')`).
665
+
If a file path is provided this way, it will be treated as absolute (no other locations are checked).
666
+
667
+
```python
668
+
from pathlib import Path
669
+
from typing import Tuple, Type
670
+
671
+
from pydantic_settings import (
672
+
BaseSettings,
673
+
PydanticBaseSettingsSource,
674
+
PyprojectTomlConfigSettingsSource,
675
+
SettingsConfigDict,
676
+
)
677
+
678
+
679
+
classDiscoverSettings(BaseSettings):
680
+
"""Example of discovering a pyproject.toml in parent directories in not in `Path.cwd()`."""
0 commit comments