Skip to content

Commit a3f5389

Browse files
committed
refactor: rename 'yaml_nested_key' -> 'yaml_config_section'
1 parent 3e39401 commit a3f5389

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

pydantic_settings/main.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ class SettingsConfigDict(ConfigDict, total=False):
6262
json_file_encoding: str | None
6363
yaml_file: PathType | None
6464
yaml_file_encoding: str | None
65-
yaml_nested_key: str | None
65+
yaml_config_section: str | None
66+
"""
67+
Specifies the top-level key in a YAML file from which to load the settings.
68+
If provided, the settings will be loaded from the nested section under this key.
69+
This is useful when the YAML file contains multiple configuration sections
70+
and you only want to load a specific subset into your settings model.
71+
"""
72+
6673
pyproject_toml_depth: int
6774
"""
6875
Number of levels **up** from the current working directory to attempt to find a pyproject.toml
@@ -447,7 +454,7 @@ def _settings_build_values(
447454
json_file_encoding=None,
448455
yaml_file=None,
449456
yaml_file_encoding=None,
450-
yaml_nested_key=None,
457+
yaml_config_section=None,
451458
toml_file=None,
452459
secrets_dir=None,
453460
protected_namespaces=('model_validate', 'model_dump', 'settings_customise_sources'),

pydantic_settings/sources/providers/yaml.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ def __init__(
3939
settings_cls: type[BaseSettings],
4040
yaml_file: PathType | None = DEFAULT_PATH,
4141
yaml_file_encoding: str | None = None,
42-
yaml_nested_key: str | None = None,
42+
yaml_config_section: str | None = None,
4343
):
4444
self.yaml_file_path = yaml_file if yaml_file != DEFAULT_PATH else settings_cls.model_config.get('yaml_file')
4545
self.yaml_file_encoding = (
4646
yaml_file_encoding
4747
if yaml_file_encoding is not None
4848
else settings_cls.model_config.get('yaml_file_encoding')
4949
)
50-
self.yaml_nested_key = (
51-
yaml_nested_key if yaml_nested_key is not None else settings_cls.model_config.get('yaml_nested_key')
50+
self.yaml_config_section = (
51+
yaml_config_section
52+
if yaml_config_section is not None
53+
else settings_cls.model_config.get('yaml_config_section')
5254
)
5355
self.yaml_data = self._read_files(self.yaml_file_path)
5456

55-
if self.yaml_nested_key:
56-
self.yaml_data = self.yaml_data[self.yaml_nested_key]
57+
if self.yaml_config_section:
58+
self.yaml_data = self.yaml_data[self.yaml_config_section]
5759
super().__init__(settings_cls, self.yaml_data)
5860

5961
def _read_file(self, file_path: Path) -> dict[str, Any]:

tests/test_source_yaml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def settings_customise_sources(
169169

170170

171171
@pytest.mark.skipif(yaml is None, reason='pyYAML is not installed')
172-
def test_yaml_nested_key(tmp_path):
172+
def test_yaml_config_section(tmp_path):
173173
p = tmp_path / '.env'
174174
p.write_text(
175175
"""
@@ -182,7 +182,7 @@ def test_yaml_nested_key(tmp_path):
182182
class Settings(BaseSettings):
183183
nested_field: str
184184

185-
model_config = SettingsConfigDict(yaml_file=p, yaml_nested_key='nested')
185+
model_config = SettingsConfigDict(yaml_file=p, yaml_config_section='nested')
186186

187187
@classmethod
188188
def settings_customise_sources(

0 commit comments

Comments
 (0)