Skip to content

Commit e66959e

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

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
@@ -35,9 +35,12 @@
3535
DotEnvSettingsSource,
3636
EnvSettingsSource,
3737
InitSettingsSource,
38+
JsonConfigSettingsSource,
3839
PydanticBaseSettingsSource,
3940
SecretsSettingsSource,
4041
SettingsConfigDict,
42+
TomlConfigSettingsSource,
43+
YamlConfigSettingsSource,
4144
)
4245
from pydantic_settings.sources import DefaultSettingsSource, SettingsError
4346

@@ -1808,6 +1811,21 @@ def test_builtins_settings_source_repr():
18081811
)
18091812

18101813

1814+
def test_repr_json_config_settings_source() -> None:
1815+
source = JsonConfigSettingsSource(BaseSettings(), Path('config.json'))
1816+
assert repr(source) == 'JsonConfigSettingsSource(json_file=config.json)'
1817+
1818+
1819+
def test_repr_toml_config_settings_source() -> None:
1820+
source = TomlConfigSettingsSource(BaseSettings(), Path('config.toml'))
1821+
assert repr(source) == 'TomlConfigSettingsSource(toml_file=config.toml)'
1822+
1823+
1824+
def test_repr_yaml_config_settings_source() -> None:
1825+
source = YamlConfigSettingsSource(BaseSettings(), Path('config.yaml'))
1826+
assert repr(source) == 'YamlConfigSettingsSource(yaml_file=config.yaml)'
1827+
1828+
18111829
def _parse_custom_dict(value: str) -> Callable[[str], Dict[int, str]]:
18121830
"""A custom parsing function passed into env parsing test."""
18131831
res = {}

0 commit comments

Comments
 (0)