Skip to content

Commit

Permalink
Define __repr__() on (Json|Toml|Yaml)ConfigSettingsSource
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dlax committed Nov 4, 2024
1 parent b2c979c commit 0e18a54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
with open(file_path, encoding=self.json_file_encoding) as json_file:
return json.load(json_file)

def __repr__(self) -> str:
return f'JsonConfigSettingsSource(json_file={self.json_file_path})'


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

def __repr__(self) -> str:
return f'TomlConfigSettingsSource(toml_file={self.toml_file_path})'


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

def __repr__(self) -> str:
return f'YamlConfigSettingsSource(yaml_file={self.yaml_file_path})'


class AzureKeyVaultMapping(Mapping[str, Optional[str]]):
_loaded_secrets: dict[str, str | None]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
DotEnvSettingsSource,
EnvSettingsSource,
InitSettingsSource,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SecretsSettingsSource,
SettingsConfigDict,
TomlConfigSettingsSource,
YamlConfigSettingsSource,
)
from pydantic_settings.sources import DefaultSettingsSource, SettingsError

Expand Down Expand Up @@ -1797,6 +1800,21 @@ def test_builtins_settings_source_repr():
)


@pytest.mark.parametrize(
'cls, expected',
(
(JsonConfigSettingsSource, 'JsonConfigSettingsSource(json_file=config.json)'),
(TomlConfigSettingsSource, 'TomlConfigSettingsSource(toml_file=config.toml)'),
(YamlConfigSettingsSource, 'YamlConfigSettingsSource(yaml_file=config.yaml)'),
),
)
def test_configfile_settings_source_repr(cls: Type[PydanticBaseSettingsSource], expected: str) -> None:
ext = cls.__name__[:4].lower()
path = Path(f'config.{ext}')
source = cls(BaseSettings(), path)
assert repr(source) == expected


def _parse_custom_dict(value: str) -> Callable[[str], Dict[int, str]]:
"""A custom parsing function passed into env parsing test."""
res = {}
Expand Down

0 comments on commit 0e18a54

Please sign in to comment.