8000 Use the class name in the `__repr__` implementations (#465) · pydantic/pydantic-settings@87ad4db · GitHub
[go: up one dir, main page]

Skip to content

Commit 87ad4db

Browse files
dlaxhramezani
andauthored
Use the class name in the __repr__ implementations (#465)
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
1 parent 9583896 commit 87ad4db

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

pydantic_settings/sources.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ def __call__(self) -> dict[str, Any]:
350350
return self.defaults
351351

352352
def __repr__(self) -> str:
353-
return f'DefaultSettingsSource(nested_model_default_partial_update={self.nested_model_default_partial_update})'
353+
return (
354+
f'{self.__class__.__name__}(nested_model_default_partial_update={self.nested_model_default_partial_update})'
355+
)
354356

355357

356358
class InitSettingsSource(PydanticBaseSettingsSource):
@@ -384,7 +386,7 @@ def __call__(self) -> dict[str, Any]:
384386
)
385387

386388
def __repr__(self) -> str:
387-
return f'InitSettingsSource(init_kwargs={self.init_kwargs!r})'
389+
return f'{self.__class__.__name__}(init_kwargs={self.init_kwargs!r})'
388390

389391

390392
class PydanticBaseEnvSettingsSource(PydanticBaseSettingsSource):
@@ -674,7 +676,7 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
674676
return None, field_key, value_is_complex
675677

676678
def __repr__(self) -> str:
677-
return f'SecretsSettingsSource(secrets_dir={self.secrets_dir!r})'
679+
return f'{self.__class__.__name__}(secrets_dir={self.secrets_dir!r})'
678680

679681

680682
class EnvSettingsSource(PydanticBaseEnvSettingsSource):
@@ -899,7 +901,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
899901

900902
def __repr__(self) -> str:
901903
return (
902-
f'EnvSettingsSource(env_nested_delimiter={self.env_nested_delimiter!r}, '
904+
f'{self.__class__.__name__}(env_nested_delimiter={self.env_nested_delimiter!r}, '
903905
f'env_prefix_len={self.env_prefix_len!r})'
904906
)
905907

@@ -1015,7 +1017,7 @@ def __call__(self) -> dict[str, Any]:
10151017

10161018
def __repr__(self) -> str:
10171019
return (
1018-
f'DotEnvSettingsSource(env_file={self.env_file!r}, env_file_encoding={self.env_file_encoding!r}, '
1020+
f'{self.__class__.__name__}(env_file={self.env_file!r}, env_file_encoding={self.env_file_encoding!r}, '
10191021
f'env_nested_delimiter={self.env_nested_delimiter!r}, env_prefix_len={self.env_prefix_len!r})'
10201022
)
10211023

@@ -1919,6 +1921,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
19191921
with open(file_path, encoding=self.json_file_encoding) as json_file:
19201922
return json.load(json_file)
19211923

1924+
def __repr__(self) -> str:
1925+
return f'{self.__class__.__name__}(json_file={self.json_file_path})'
1926+
19221927

19231928
class TomlConfigSettingsSource(InitSettingsSource, ConfigFileSourceMixin):
19241929
"""
@@ -1941,6 +1946,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
19411946
return tomli.load(toml_file)
19421947
return tomllib.load(toml_file)
19431948

1949+
def __repr__(self) -> str:
1950+
return f'{self.__class__.__name__}(toml_file={self.toml_file_path})'
1951+
19441952

19451953
class PyprojectTomlConfigSettingsSource(TomlConfigSettingsSource):
19461954
"""
@@ -2013,6 +2021,9 @@ def _read_file(self, file_path: Path) -> dict[str, Any]:
20132021
with open(file_path, encoding=self.yaml_file_encoding) as yaml_file:
20142022
return yaml.safe_load(yaml_file) or {}
20152023

2024+
def __repr__(self) -> str:
2025+
return f'{self.__class__.__name__}(yaml_file={self.yaml_file_path})'
2026+
20162027

20172028
class AzureKeyVaultMapping(Mapping[str, Optional[str]]):
20182029
_loaded_secrets: dict[str, str | None]
@@ -2075,7 +2086,7 @@ def _load_env_vars(self) -> Mapping[str, Optional[str]]:
20752086
return AzureKeyVaultMapping(secret_client)
20762087

20772088
def __repr__(self) -> str:
2078-
return f'AzureKeyVaultSettingsSource(url={self._url!r}, ' f'env_nested_delimiter={self.env_nested_delimiter!r})'
2089+
return f'{self.__class__.__name__}(url={self._url!r}, ' f'env_nested_delimiter={self.env_nested_delimiter!r})'
20792090

20802091

20812092
def _get_env_var_key(key: str, case_sensitive: bool = False) -> str:

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)
0