diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index 2688a049..203e1037 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -350,7 +350,9 @@ def __call__(self) -> dict[str, Any]: return self.defaults def __repr__(self) -> str: - return f'DefaultSettingsSource(nested_model_default_partial_update={self.nested_model_default_partial_update})' + return ( + f'{self.__class__.__name__}(nested_model_default_partial_update={self.nested_model_default_partial_update})' + ) class InitSettingsSource(PydanticBaseSettingsSource): @@ -384,7 +386,7 @@ def __call__(self) -> dict[str, Any]: ) def __repr__(self) -> str: - return f'InitSettingsSource(init_kwargs={self.init_kwargs!r})' + return f'{self.__class__.__name__}(init_kwargs={self.init_kwargs!r})' class PydanticBaseEnvSettingsSource(PydanticBaseSettingsSource): @@ -674,7 +676,7 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, return None, field_key, value_is_complex def __repr__(self) -> str: - return f'SecretsSettingsSource(secrets_dir={self.secrets_dir!r})' + return f'{self.__class__.__name__}(secrets_dir={self.secrets_dir!r})' class EnvSettingsSource(PydanticBaseEnvSettingsSource): @@ -899,7 +901,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[ def __repr__(self) -> str: return ( - f'EnvSettingsSource(env_nested_delimiter={self.env_nested_delimiter!r}, ' + f'{self.__class__.__name__}(env_nested_delimiter={self.env_nested_delimiter!r}, ' f'env_prefix_len={self.env_prefix_len!r})' ) @@ -1015,7 +1017,7 @@ def __call__(self) -> dict[str, Any]: def __repr__(self) -> str: return ( - f'DotEnvSettingsSource(env_file={self.env_file!r}, env_file_encoding={self.env_file_encoding!r}, ' + f'{self.__class__.__name__}(env_file={self.env_file!r}, env_file_encoding={self.env_file_encoding!r}, ' f'env_nested_delimiter={self.env_nested_delimiter!r}, env_prefix_len={self.env_prefix_len!r})' ) @@ -1919,6 +1921,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'{self.__class__.__name__}(json_file={self.json_file_path})' + class TomlConfigSettingsSource(InitSettingsSource, ConfigFileSourceMixin): """ @@ -1941,6 +1946,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'{self.__class__.__name__}(toml_file={self.toml_file_path})' + class PyprojectTomlConfigSettingsSource(TomlConfigSettingsSource): """ @@ -2013,6 +2021,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'{self.__class__.__name__}(yaml_file={self.yaml_file_path})' + class AzureKeyVaultMapping(Mapping[str, Optional[str]]): _loaded_secrets: dict[str, str | None] @@ -2075,7 +2086,7 @@ def _load_env_vars(self) -> Mapping[str, Optional[str]]: return AzureKeyVaultMapping(secret_client) def __repr__(self) -> str: - return f'AzureKeyVaultSettingsSource(url={self._url!r}, ' f'env_nested_delimiter={self.env_nested_delimiter!r})' + return f'{self.__class__.__name__}(url={self._url!r}, ' f'env_nested_delimiter={self.env_nested_delimiter!r})' def _get_env_var_key(key: str, case_sensitive: bool = False) -> str: diff --git a/tests/test_source_json.py b/tests/test_source_json.py index e348a6b6..c6360f95 100644 --- a/tests/test_source_json.py +++ b/tests/test_source_json.py @@ -3,6 +3,7 @@ """ import json +from pathlib import Path from typing import Tuple, Type, Union from pydantic import BaseModel @@ -15,6 +16,11 @@ ) +def test_repr() -> None: + source = JsonConfigSettingsSource(BaseSettings(), Path('config.json')) + assert repr(source) == 'JsonConfigSettingsSource(json_file=config.json)' + + def test_json_file(tmp_path): p = tmp_path / '.env' p.write_text( diff --git a/tests/test_source_toml.py b/tests/test_source_toml.py index 29186018..a7230ce2 100644 --- a/tests/test_source_toml.py +++ b/tests/test_source_toml.py @@ -3,6 +3,7 @@ """ import sys +from pathlib import Path from typing import Tuple, Type import pytest @@ -21,6 +22,11 @@ tomli = None +def test_repr() -> None: + source = TomlConfigSettingsSource(BaseSettings(), Path('config.toml')) + assert repr(source) == 'TomlConfigSettingsSource(toml_file=config.toml)' + + @pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed') def test_toml_file(tmp_path): p = tmp_path / '.env' diff --git a/tests/test_source_yaml.py b/tests/test_source_yaml.py index fd25de67..1929b9dc 100644 --- a/tests/test_source_yaml.py +++ b/tests/test_source_yaml.py @@ -2,6 +2,7 @@ Test pydantic_settings.YamlConfigSettingsSource. """ +from pathlib import Path from typing import Tuple, Type, Union import pytest @@ -20,6 +21,11 @@ yaml = None +def test_repr() -> None: + source = YamlConfigSettingsSource(BaseSettings(), Path('config.yaml')) + assert repr(source) == 'YamlConfigSettingsSource(yaml_file=config.yaml)' + + @pytest.mark.skipif(yaml, reason='PyYAML is installed') def test_yaml_not_installed(tmp_path): p = tmp_path / '.env'