Skip to content

Commit 0e18a54

Browse files
committed
Define __repr__() on (Json|Toml|Yaml)ConfigSettingsSource
Previously, the repr() of those settings source classes was inherited from InitSettingsSource's thus returning a misleading 'InitSettingsSource(init_kwargs={})'. We define specific a __repr__() method implementation for classes using a config file (json, toml, yaml).
1 parent b2c979c commit 0e18a54

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pydantic_settings/sources.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
19511951
with open(file_path, encoding=self.json_file_encoding) as json_file:
19521952
return json.load(json_file)
19531953

1954+
def __repr__(self) -> str:
1955+
return f'JsonConfigSettingsSource(json_file={self.json_file_path})'
1956+
19541957

19551958
class TomlConfigSettingsSource(InitSettingsSource, ConfigFileSourceMixin):
19561959
"""
@@ -1973,6 +1976,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
19731976
return tomli.load(toml_file)
19741977
return tomllib.load(toml_file)
19751978

1979+
def __repr__(self) -> str:
1980+
return f'TomlConfigSettingsSource(toml_file={self.toml_file_path})'
1981+
19761982

19771983
class PyprojectTomlConfigSettingsSource(TomlConfigSettingsSource):
19781984
"""
@@ -2045,6 +2051,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
20452051
with open(file_path, encoding=self.yaml_file_encoding) as yaml_file:
20462052
return yaml.safe_load(yaml_file) or {}
20472053

2054+
def __repr__(self) -> str:
2055+
return f'YamlConfigSettingsSource(yaml_file={self.yaml_file_path})'
2056+
20482057

20492058
class AzureKeyVaultMapping(Mapping[str, Optional[str]]):
20502059
_loaded_secrets: dict[str, str | None]

tests/test_settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
DotEnvSettingsSource,
3535
EnvSettingsSource,
3636
InitSettingsSource,
37+
JsonConfigSettingsSource,
3738
PydanticBaseSettingsSource,
3839
SecretsSettingsSource,
3940
SettingsConfigDict,
41+
TomlConfigSettingsSource,
42+
YamlConfigSettingsSource,
4043
)
4144
from pydantic_settings.sources import DefaultSettingsSource, SettingsError
4245

@@ -1797,6 +1800,21 @@ def test_builtins_settings_source_repr():
17971800
)
17981801

17991802

1803+
@pytest.mark.parametrize(
1804+
'cls, expected',
1805+
(
1806+
(JsonConfigSettingsSource, 'JsonConfigSettingsSource(json_file=config.json)'),
1807+
(TomlConfigSettingsSource, 'TomlConfigSettingsSource(toml_file=config.toml)'),
1808+
(YamlConfigSettingsSource, 'YamlConfigSettingsSource(yaml_file=config.yaml)'),
1809+
),
1810+
)
1811+
def test_configfile_settings_source_repr(cls: Type[PydanticBaseSettingsSource], expected: str) -> None:
1812+
ext = cls.__name__[:4].lower()
1813+
path = Path(f'config.{ext}')
1814+
source = cls(BaseSettings(), path)
1815+
assert repr(source) == expected
1816+
1817+
18001818
def _parse_custom_dict(value: str) -> Callable[[str], Dict[int, str]]:
18011819
"""A custom parsing function passed into env parsing test."""
18021820
res = {}

0 commit comments

Comments
 (0)