Skip to content

Commit 21b66b0

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 21b66b0

File tree

4 files changed

+27
-0
lines changed

4 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_source_json.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import json
6+
from pathlib import Path
67
from typing import Tuple, Type, Union
78

89
from pydantic import BaseModel
@@ -15,6 +16,11 @@
1516
)
1617

1718

19+
def test_repr() -> None:
20+
source = JsonConfigSettingsSource(BaseSettings(), Path('config.json'))
21+
assert repr(source) == 'JsonConfigSettingsSource(json_file=config.json)'
22+
23+
1824
def test_json_file(tmp_path):
1925
p = tmp_path / '.env'
2026
p.write_text(

tests/test_source_toml.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import sys
6+
from pathlib import Path
67
from typing import Tuple, Type
78

89
import pytest
@@ -21,6 +22,11 @@
2122
tomli = None
2223

2324

25+
def test_repr() -> None:
26+
source = TomlConfigSettingsSource(BaseSettings(), Path('config.toml'))
27+
assert repr(source) == 'TomlConfigSettingsSource(toml_file=config.toml)'
28+
29+
2430
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
2531
def test_toml_file(tmp_path):
2632
p = tmp_path / '.env'

tests/test_source_yaml.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test pydantic_settings.YamlConfigSettingsSource.
33
"""
44

5+
from pathlib import Path
56
from typing import Tuple, Type, Union
67

78
import pytest
@@ -20,6 +21,11 @@
2021
yaml = None
2122

2223

24+
def test_repr() -> None:
25+
source = YamlConfigSettingsSource(BaseSettings(), Path('config.yaml'))
26+
assert repr(source) == 'YamlConfigSettingsSource(yaml_file=config.yaml)'
27+
28+
2329
@pytest.mark.skipif(yaml, reason='PyYAML is installed')
2430
def test_yaml_not_installed(tmp_path):
2531
p = tmp_path / '.env'

0 commit comments

Comments
 (0)