Skip to content

Commit

Permalink
Use the class name in the __repr__ implementations (#465)
Browse files Browse the repository at this point in the history
Co-authored-by: Hasan Ramezani <[email protected]>
  • Loading branch information
dlax and hramezani authored Nov 6, 2024
1 parent 9583896 commit 87ad4db
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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})'
)

Expand Down Expand Up @@ -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})'
)

Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_source_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import json
from pathlib import Path
from typing import Tuple, Type, Union

from pydantic import BaseModel
Expand All @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_source_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import sys
from pathlib import Path
from typing import Tuple, Type

import pytest
Expand All @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions tests/test_source_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Test pydantic_settings.YamlConfigSettingsSource.
"""

from pathlib import Path
from typing import Tuple, Type, Union

import pytest
Expand All @@ -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'
Expand Down

0 comments on commit 87ad4db

Please sign in to comment.