Skip to content

Commit

Permalink
Fix repr() for (Json|Toml|Yaml)ConfigSettingsSource
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dlax committed Nov 6, 2024
1 parent 0922bc1 commit e66959e
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 @@ -35,9 +35,12 @@
DotEnvSettingsSource,
EnvSettingsSource,
InitSettingsSource,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SecretsSettingsSource,
SettingsConfigDict,
TomlConfigSettingsSource,
YamlConfigSettingsSource,
)
from pydantic_settings.sources import DefaultSettingsSource, SettingsError

Expand Down Expand Up @@ -1808,6 +1811,21 @@ def test_builtins_settings_source_repr():
)


def test_repr_json_config_settings_source() -> None:
source = JsonConfigSettingsSource(BaseSettings(), Path('config.json'))
assert repr(source) == 'JsonConfigSettingsSource(json_file=config.json)'


def test_repr_toml_config_settings_source() -> None:
source = TomlConfigSettingsSource(BaseSettings(), Path('config.toml'))
assert repr(source) == 'TomlConfigSettingsSource(toml_file=config.toml)'


def test_repr_yaml_config_settings_source() -> None:
source = YamlConfigSettingsSource(BaseSettings(), Path('config.yaml'))
assert repr(source) == 'YamlConfigSettingsSource(yaml_file=config.yaml)'


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 e66959e

Please sign in to comment.