Coverage for pydantic/_internal/_repr.py: 100.00%
66 statements
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-21 17:00 +0000
« prev ^ index » next coverage.py v7.5.3, created at 2024-06-21 17:00 +0000
1"""Tools to provide pretty/human-readable display of objects."""
3from __future__ import annotations as _annotations 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
5import types 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
6import typing 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
7from typing import Any 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
9import typing_extensions 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
11from . import _typing_extra 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
13if typing.TYPE_CHECKING: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
14 ReprArgs: typing_extensions.TypeAlias = 'typing.Iterable[tuple[str | None, Any]]'
15 RichReprResult: typing_extensions.TypeAlias = (
16 'typing.Iterable[Any | tuple[Any] | tuple[str, Any] | tuple[str, Any, Any]]'
17 )
20class PlainRepr(str): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
21 """String class where repr doesn't include quotes. Useful with Representation when you want to return a string
22 representation of something that is valid (or pseudo-valid) python.
23 """
25 def __repr__(self) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
26 return str(self) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
29class Representation: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
30 # Mixin to provide `__str__`, `__repr__`, and `__pretty__` and `__rich_repr__` methods.
31 # `__pretty__` is used by [devtools](https://python-devtools.helpmanual.io/).
32 # `__rich_repr__` is used by [rich](https://rich.readthedocs.io/en/stable/pretty.html).
33 # (this is not a docstring to avoid adding a docstring to classes which inherit from Representation)
35 # we don't want to use a type annotation here as it can break get_type_hints
36 __slots__ = tuple() # type: typing.Collection[str] 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
38 def __repr_args__(self) -> ReprArgs: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
39 """Returns the attributes to show in __str__, __repr__, and __pretty__ this is generally overridden.
41 Can either return:
42 * name - value pairs, e.g.: `[('foo_name', 'foo'), ('bar_name', ['b', 'a', 'r'])]`
43 * or, just values, e.g.: `[(None, 'foo'), (None, ['b', 'a', 'r'])]`
44 """
45 attrs_names = self.__slots__ 1ABtughijabvkCDwxlmnocdEFyzpqrsef
46 if not attrs_names and hasattr(self, '__dict__'): 1ABtughijabvkCDwxlmnocdEFyzpqrsef
47 attrs_names = self.__dict__.keys() 1ABtughijabvkCDwxlmnocdEFyzpqrsef
48 attrs = ((s, getattr(self, s)) for s in attrs_names) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
49 return [(a, v) for a, v in attrs if v is not None] 1ABtughijabvkCDwxlmnocdEFyzpqrsef
51 def __repr_name__(self) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
52 """Name of the instance's class, used in __repr__."""
53 return self.__class__.__name__ 1ABtughijabvkCDwxlmnocdEFyzpqrsef
55 def __repr_str__(self, join_str: str) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
56 return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__()) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
58 def __pretty__(self, fmt: typing.Callable[[Any], Any], **kwargs: Any) -> typing.Generator[Any, None, None]: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
59 """Used by devtools (https://python-devtools.helpmanual.io/) to pretty print objects."""
60 yield self.__repr_name__() + '(' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
61 yield 1 1ABtughijabvkCDwxlmnocdEFyzpqrsef
62 for name, value in self.__repr_args__(): 1ABtughijabvkCDwxlmnocdEFyzpqrsef
63 if name is not None: 1ABtughijabvkCDwxlmnocdEFyzpqrsef
64 yield name + '=' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
65 yield fmt(value) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
66 yield ',' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
67 yield 0 1ABtughijabvkCDwxlmnocdEFyzpqrsef
68 yield -1 1ABtughijabvkCDwxlmnocdEFyzpqrsef
69 yield ')' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
71 def __rich_repr__(self) -> RichReprResult: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
72 """Used by Rich (https://rich.readthedocs.io/en/stable/pretty.html) to pretty print objects."""
73 for name, field_repr in self.__repr_args__(): 1ABtughijabvkCDwxlmnocdEFyzpqrsef
74 if name is None: 1ABtughijabvkCDwxlmnocdEFyzpqrsef
75 yield field_repr 1ABtughijabvkCDwxlmnocdEFyzpqrsef
76 else:
77 yield name, field_repr 1ABtughijabvkCDwxlmnocdEFyzpqrsef
79 def __str__(self) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
80 return self.__repr_str__(' ') 1ABtughijabvkCDwxlmnocdEFyzpqrsef
82 def __repr__(self) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
83 return f'{self.__repr_name__()}({self.__repr_str__(", ")})' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
86def display_as_type(obj: Any) -> str: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
87 """Pretty representation of a type, should be as close as possible to the original type definition string.
89 Takes some logic from `typing._type_repr`.
90 """
91 if isinstance(obj, types.FunctionType): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
92 return obj.__name__ 1ABtughijabvkCDwxlmnocdEFyzpqrsef
93 elif obj is ...: 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
94 return '...' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
95 elif isinstance(obj, Representation): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
96 return repr(obj) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
97 elif isinstance(obj, typing_extensions.TypeAliasType): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
98 return str(obj) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
100 if not isinstance(obj, (_typing_extra.typing_base, _typing_extra.WithArgsTypes, type)): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
101 obj = obj.__class__ 1ABtughijabvkCDwxlmnocdEFyzpqrsef
103 if _typing_extra.origin_is_union(typing_extensions.get_origin(obj)): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
104 args = ', '.join(map(display_as_type, typing_extensions.get_args(obj))) 1ABtughijabvkCDwxlmnocdEFyzpqrsef
105 return f'Union[{args}]' 1ABtughijabvkCDwxlmnocdEFyzpqrsef
106 elif isinstance(obj, _typing_extra.WithArgsTypes): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
107 if typing_extensions.get_origin(obj) == typing_extensions.Literal: 1tughijabvkwxlmnocdyzpqrsef
108 args = ', '.join(map(repr, typing_extensions.get_args(obj))) 1ghijabklmnocdpqrsef
109 else:
110 args = ', '.join(map(display_as_type, typing_extensions.get_args(obj))) 1tughijabvkwxlmnocdyzpqrsef
111 try: 1tughijabvkwxlmnocdyzpqrsef
112 return f'{obj.__qualname__}[{args}]' 1tughijabvkwxlmnocdyzpqrsef
113 except AttributeError: 1abcdef
114 return str(obj) # handles TypeAliasType in 3.12 1abcdef
115 elif isinstance(obj, type): 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
116 return obj.__qualname__ 1ABtughijabvkCDwxlmnocdGHIJKLMNOEFyzpqrsef
117 else:
118 return repr(obj).replace('typing.', '').replace('typing_extensions.', '') 1ABtughijabvkCDwxlmnocdEFyzpqrsef