Coverage for pydantic/_internal/_repr.py: 100.00%
70 statements
« prev ^ index » next coverage.py v7.10.0, created at 2025-07-26 11:49 +0000
« prev ^ index » next coverage.py v7.10.0, created at 2025-07-26 11:49 +0000
1"""Tools to provide pretty/human-readable display of objects."""
3from __future__ import annotations as _annotations 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
5import types 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
6import typing 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
7from typing import Any 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
9import typing_extensions 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
10from typing_inspection import typing_objects 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
11from typing_inspection.introspection import is_union_origin 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
13from . import _typing_extra 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
15if typing.TYPE_CHECKING: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
16 ReprArgs: typing_extensions.TypeAlias = 'typing.Iterable[tuple[str | None, Any]]'
17 RichReprResult: typing_extensions.TypeAlias = (
18 'typing.Iterable[Any | tuple[Any] | tuple[str, Any] | tuple[str, Any, Any]]'
19 )
22class PlainRepr(str): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
23 """String class where repr doesn't include quotes. Useful with Representation when you want to return a string
24 representation of something that is valid (or pseudo-valid) python.
25 """
27 def __repr__(self) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
28 return str(self) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
31class Representation: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
32 # Mixin to provide `__str__`, `__repr__`, and `__pretty__` and `__rich_repr__` methods.
33 # `__pretty__` is used by [devtools](https://python-devtools.helpmanual.io/).
34 # `__rich_repr__` is used by [rich](https://rich.readthedocs.io/en/stable/pretty.html).
35 # (this is not a docstring to avoid adding a docstring to classes which inherit from Representation)
37 # we don't want to use a type annotation here as it can break get_type_hints
38 __slots__ = () # type: typing.Collection[str] 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
40 def __repr_args__(self) -> ReprArgs: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
41 """Returns the attributes to show in __str__, __repr__, and __pretty__ this is generally overridden.
43 Can either return:
44 * name - value pairs, e.g.: `[('foo_name', 'foo'), ('bar_name', ['b', 'a', 'r'])]`
45 * or, just values, e.g.: `[(None, 'foo'), (None, ['b', 'a', 'r'])]`
46 """
47 attrs_names = self.__slots__ 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
48 if not attrs_names and hasattr(self, '__dict__'): 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
49 attrs_names = self.__dict__.keys() 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
50 attrs = ((s, getattr(self, s)) for s in attrs_names) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
51 return [(a, v if v is not self else self.__repr_recursion__(v)) for a, v in attrs if v is not None] 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
53 def __repr_name__(self) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
54 """Name of the instance's class, used in __repr__."""
55 return self.__class__.__name__ 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
57 def __repr_recursion__(self, object: Any) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
58 """Returns the string representation of a recursive object."""
59 # This is copied over from the stdlib `pprint` module:
60 return f'<Recursion on {type(object).__name__} with id={id(object)}>' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
62 def __repr_str__(self, join_str: str) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
63 return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__()) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
65 def __pretty__(self, fmt: typing.Callable[[Any], Any], **kwargs: Any) -> typing.Generator[Any, None, None]: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
66 """Used by devtools (https://python-devtools.helpmanual.io/) to pretty print objects."""
67 yield self.__repr_name__() + '(' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
68 yield 1 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
69 for name, value in self.__repr_args__(): 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
70 if name is not None: 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
71 yield name + '=' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
72 yield fmt(value) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
73 yield ',' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
74 yield 0 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
75 yield -1 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
76 yield ')' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
78 def __rich_repr__(self) -> RichReprResult: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
79 """Used by Rich (https://rich.readthedocs.io/en/stable/pretty.html) to pretty print objects."""
80 for name, field_repr in self.__repr_args__(): 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
81 if name is None: 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
82 yield field_repr 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
83 else:
84 yield name, field_repr 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
86 def __str__(self) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
87 return self.__repr_str__(' ') 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
89 def __repr__(self) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
90 return f'{self.__repr_name__()}({self.__repr_str__(", ")})' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
93def display_as_type(obj: Any) -> str: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
94 """Pretty representation of a type, should be as close as possible to the original type definition string.
96 Takes some logic from `typing._type_repr`.
97 """
98 if isinstance(obj, (types.FunctionType, types.BuiltinFunctionType)): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
99 return obj.__name__ 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
100 elif obj is ...: 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
101 return '...' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
102 elif isinstance(obj, Representation): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
103 return repr(obj) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
104 elif isinstance(obj, typing.ForwardRef) or typing_objects.is_typealiastype(obj): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
105 return str(obj) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
107 if not isinstance(obj, (_typing_extra.typing_base, _typing_extra.WithArgsTypes, type)): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
108 obj = obj.__class__ 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
110 if is_union_origin(typing_extensions.get_origin(obj)): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
111 args = ', '.join(map(display_as_type, typing_extensions.get_args(obj))) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
112 return f'Union[{args}]' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
113 elif isinstance(obj, _typing_extra.WithArgsTypes): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
114 if typing_objects.is_literal(typing_extensions.get_origin(obj)): 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
115 args = ', '.join(map(repr, typing_extensions.get_args(obj))) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
116 else:
117 args = ', '.join(map(display_as_type, typing_extensions.get_args(obj))) 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
118 try: 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
119 return f'{obj.__qualname__}[{args}]' 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO
120 except AttributeError: 1abcdefg
121 return str(obj).replace('typing.', '').replace('typing_extensions.', '') # handles TypeAliasType in 3.12 1abcdefg
122 elif isinstance(obj, type): 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
123 return obj.__qualname__ 1abhijklmnopqrcsdetuvwxyzABCDPfgEFGHIJKLMNO
124 else:
125 return repr(obj).replace('typing.', '').replace('typing_extensions.', '') 1abhijklmnopqrcsdetuvwxyzABCDfgEFGHIJKLMNO