pretty.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. import builtins
  2. import collections
  3. import dataclasses
  4. import inspect
  5. import os
  6. import sys
  7. from array import array
  8. from collections import Counter, UserDict, UserList, defaultdict, deque
  9. from dataclasses import dataclass, fields, is_dataclass
  10. from inspect import isclass
  11. from itertools import islice
  12. from types import MappingProxyType
  13. from typing import (
  14. TYPE_CHECKING,
  15. Any,
  16. Callable,
  17. DefaultDict,
  18. Dict,
  19. Iterable,
  20. List,
  21. Optional,
  22. Sequence,
  23. Set,
  24. Tuple,
  25. Union,
  26. )
  27. from pip._vendor.rich.repr import RichReprResult
  28. try:
  29. import attr as _attr_module
  30. _has_attrs = hasattr(_attr_module, "ib")
  31. except ImportError: # pragma: no cover
  32. _has_attrs = False
  33. from . import get_console
  34. from ._loop import loop_last
  35. from ._pick import pick_bool
  36. from .abc import RichRenderable
  37. from .cells import cell_len
  38. from .highlighter import ReprHighlighter
  39. from .jupyter import JupyterMixin, JupyterRenderable
  40. from .measure import Measurement
  41. from .text import Text
  42. if TYPE_CHECKING:
  43. from .console import (
  44. Console,
  45. ConsoleOptions,
  46. HighlighterType,
  47. JustifyMethod,
  48. OverflowMethod,
  49. RenderResult,
  50. )
  51. def _is_attr_object(obj: Any) -> bool:
  52. """Check if an object was created with attrs module."""
  53. return _has_attrs and _attr_module.has(type(obj))
  54. def _get_attr_fields(obj: Any) -> Sequence["_attr_module.Attribute[Any]"]:
  55. """Get fields for an attrs object."""
  56. return _attr_module.fields(type(obj)) if _has_attrs else []
  57. def _is_dataclass_repr(obj: object) -> bool:
  58. """Check if an instance of a dataclass contains the default repr.
  59. Args:
  60. obj (object): A dataclass instance.
  61. Returns:
  62. bool: True if the default repr is used, False if there is a custom repr.
  63. """
  64. # Digging in to a lot of internals here
  65. # Catching all exceptions in case something is missing on a non CPython implementation
  66. try:
  67. return obj.__repr__.__code__.co_filename == dataclasses.__file__
  68. except Exception: # pragma: no coverage
  69. return False
  70. _dummy_namedtuple = collections.namedtuple("_dummy_namedtuple", [])
  71. def _has_default_namedtuple_repr(obj: object) -> bool:
  72. """Check if an instance of namedtuple contains the default repr
  73. Args:
  74. obj (object): A namedtuple
  75. Returns:
  76. bool: True if the default repr is used, False if there's a custom repr.
  77. """
  78. obj_file = None
  79. try:
  80. obj_file = inspect.getfile(obj.__repr__)
  81. except (OSError, TypeError):
  82. # OSError handles case where object is defined in __main__ scope, e.g. REPL - no filename available.
  83. # TypeError trapped defensively, in case of object without filename slips through.
  84. pass
  85. default_repr_file = inspect.getfile(_dummy_namedtuple.__repr__)
  86. return obj_file == default_repr_file
  87. def _ipy_display_hook(
  88. value: Any,
  89. console: Optional["Console"] = None,
  90. overflow: "OverflowMethod" = "ignore",
  91. crop: bool = False,
  92. indent_guides: bool = False,
  93. max_length: Optional[int] = None,
  94. max_string: Optional[int] = None,
  95. max_depth: Optional[int] = None,
  96. expand_all: bool = False,
  97. ) -> Union[str, None]:
  98. # needed here to prevent circular import:
  99. from .console import ConsoleRenderable
  100. # always skip rich generated jupyter renderables or None values
  101. if _safe_isinstance(value, JupyterRenderable) or value is None:
  102. return None
  103. console = console or get_console()
  104. with console.capture() as capture:
  105. # certain renderables should start on a new line
  106. if _safe_isinstance(value, ConsoleRenderable):
  107. console.line()
  108. console.print(
  109. value
  110. if _safe_isinstance(value, RichRenderable)
  111. else Pretty(
  112. value,
  113. overflow=overflow,
  114. indent_guides=indent_guides,
  115. max_length=max_length,
  116. max_string=max_string,
  117. max_depth=max_depth,
  118. expand_all=expand_all,
  119. margin=12,
  120. ),
  121. crop=crop,
  122. new_line_start=True,
  123. end="",
  124. )
  125. # strip trailing newline, not usually part of a text repr
  126. # I'm not sure if this should be prevented at a lower level
  127. return capture.get().rstrip("\n")
  128. def _safe_isinstance(
  129. obj: object, class_or_tuple: Union[type, Tuple[type, ...]]
  130. ) -> bool:
  131. """isinstance can fail in rare cases, for example types with no __class__"""
  132. try:
  133. return isinstance(obj, class_or_tuple)
  134. except Exception:
  135. return False
  136. def install(
  137. console: Optional["Console"] = None,
  138. overflow: "OverflowMethod" = "ignore",
  139. crop: bool = False,
  140. indent_guides: bool = False,
  141. max_length: Optional[int] = None,
  142. max_string: Optional[int] = None,
  143. max_depth: Optional[int] = None,
  144. expand_all: bool = False,
  145. ) -> None:
  146. """Install automatic pretty printing in the Python REPL.
  147. Args:
  148. console (Console, optional): Console instance or ``None`` to use global console. Defaults to None.
  149. overflow (Optional[OverflowMethod], optional): Overflow method. Defaults to "ignore".
  150. crop (Optional[bool], optional): Enable cropping of long lines. Defaults to False.
  151. indent_guides (bool, optional): Enable indentation guides. Defaults to False.
  152. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  153. Defaults to None.
  154. max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
  155. max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
  156. expand_all (bool, optional): Expand all containers. Defaults to False.
  157. max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
  158. """
  159. from pip._vendor.rich import get_console
  160. console = console or get_console()
  161. assert console is not None
  162. def display_hook(value: Any) -> None:
  163. """Replacement sys.displayhook which prettifies objects with Rich."""
  164. if value is not None:
  165. assert console is not None
  166. builtins._ = None # type: ignore[attr-defined]
  167. console.print(
  168. value
  169. if _safe_isinstance(value, RichRenderable)
  170. else Pretty(
  171. value,
  172. overflow=overflow,
  173. indent_guides=indent_guides,
  174. max_length=max_length,
  175. max_string=max_string,
  176. max_depth=max_depth,
  177. expand_all=expand_all,
  178. ),
  179. crop=crop,
  180. )
  181. builtins._ = value # type: ignore[attr-defined]
  182. if "get_ipython" in globals():
  183. ip = get_ipython() # type: ignore[name-defined]
  184. from IPython.core.formatters import BaseFormatter
  185. class RichFormatter(BaseFormatter): # type: ignore[misc]
  186. pprint: bool = True
  187. def __call__(self, value: Any) -> Any:
  188. if self.pprint:
  189. return _ipy_display_hook(
  190. value,
  191. console=get_console(),
  192. overflow=overflow,
  193. indent_guides=indent_guides,
  194. max_length=max_length,
  195. max_string=max_string,
  196. max_depth=max_depth,
  197. expand_all=expand_all,
  198. )
  199. else:
  200. return repr(value)
  201. # replace plain text formatter with rich formatter
  202. rich_formatter = RichFormatter()
  203. ip.display_formatter.formatters["text/plain"] = rich_formatter
  204. else:
  205. sys.displayhook = display_hook
  206. class Pretty(JupyterMixin):
  207. """A rich renderable that pretty prints an object.
  208. Args:
  209. _object (Any): An object to pretty print.
  210. highlighter (HighlighterType, optional): Highlighter object to apply to result, or None for ReprHighlighter. Defaults to None.
  211. indent_size (int, optional): Number of spaces in indent. Defaults to 4.
  212. justify (JustifyMethod, optional): Justify method, or None for default. Defaults to None.
  213. overflow (OverflowMethod, optional): Overflow method, or None for default. Defaults to None.
  214. no_wrap (Optional[bool], optional): Disable word wrapping. Defaults to False.
  215. indent_guides (bool, optional): Enable indentation guides. Defaults to False.
  216. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  217. Defaults to None.
  218. max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.
  219. max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
  220. expand_all (bool, optional): Expand all containers. Defaults to False.
  221. margin (int, optional): Subtrace a margin from width to force containers to expand earlier. Defaults to 0.
  222. insert_line (bool, optional): Insert a new line if the output has multiple new lines. Defaults to False.
  223. """
  224. def __init__(
  225. self,
  226. _object: Any,
  227. highlighter: Optional["HighlighterType"] = None,
  228. *,
  229. indent_size: int = 4,
  230. justify: Optional["JustifyMethod"] = None,
  231. overflow: Optional["OverflowMethod"] = None,
  232. no_wrap: Optional[bool] = False,
  233. indent_guides: bool = False,
  234. max_length: Optional[int] = None,
  235. max_string: Optional[int] = None,
  236. max_depth: Optional[int] = None,
  237. expand_all: bool = False,
  238. margin: int = 0,
  239. insert_line: bool = False,
  240. ) -> None:
  241. self._object = _object
  242. self.highlighter = highlighter or ReprHighlighter()
  243. self.indent_size = indent_size
  244. self.justify: Optional["JustifyMethod"] = justify
  245. self.overflow: Optional["OverflowMethod"] = overflow
  246. self.no_wrap = no_wrap
  247. self.indent_guides = indent_guides
  248. self.max_length = max_length
  249. self.max_string = max_string
  250. self.max_depth = max_depth
  251. self.expand_all = expand_all
  252. self.margin = margin
  253. self.insert_line = insert_line
  254. def __rich_console__(
  255. self, console: "Console", options: "ConsoleOptions"
  256. ) -> "RenderResult":
  257. pretty_str = pretty_repr(
  258. self._object,
  259. max_width=options.max_width - self.margin,
  260. indent_size=self.indent_size,
  261. max_length=self.max_length,
  262. max_string=self.max_string,
  263. max_depth=self.max_depth,
  264. expand_all=self.expand_all,
  265. )
  266. pretty_text = Text.from_ansi(
  267. pretty_str,
  268. justify=self.justify or options.justify,
  269. overflow=self.overflow or options.overflow,
  270. no_wrap=pick_bool(self.no_wrap, options.no_wrap),
  271. style="pretty",
  272. )
  273. pretty_text = (
  274. self.highlighter(pretty_text)
  275. if pretty_text
  276. else Text(
  277. f"{type(self._object)}.__repr__ returned empty string",
  278. style="dim italic",
  279. )
  280. )
  281. if self.indent_guides and not options.ascii_only:
  282. pretty_text = pretty_text.with_indent_guides(
  283. self.indent_size, style="repr.indent"
  284. )
  285. if self.insert_line and "\n" in pretty_text:
  286. yield ""
  287. yield pretty_text
  288. def __rich_measure__(
  289. self, console: "Console", options: "ConsoleOptions"
  290. ) -> "Measurement":
  291. pretty_str = pretty_repr(
  292. self._object,
  293. max_width=options.max_width,
  294. indent_size=self.indent_size,
  295. max_length=self.max_length,
  296. max_string=self.max_string,
  297. max_depth=self.max_depth,
  298. expand_all=self.expand_all,
  299. )
  300. text_width = (
  301. max(cell_len(line) for line in pretty_str.splitlines()) if pretty_str else 0
  302. )
  303. return Measurement(text_width, text_width)
  304. def _get_braces_for_defaultdict(_object: DefaultDict[Any, Any]) -> Tuple[str, str, str]:
  305. return (
  306. f"defaultdict({_object.default_factory!r}, {{",
  307. "})",
  308. f"defaultdict({_object.default_factory!r}, {{}})",
  309. )
  310. def _get_braces_for_array(_object: "array[Any]") -> Tuple[str, str, str]:
  311. return (f"array({_object.typecode!r}, [", "])", f"array({_object.typecode!r})")
  312. _BRACES: Dict[type, Callable[[Any], Tuple[str, str, str]]] = {
  313. os._Environ: lambda _object: ("environ({", "})", "environ({})"),
  314. array: _get_braces_for_array,
  315. defaultdict: _get_braces_for_defaultdict,
  316. Counter: lambda _object: ("Counter({", "})", "Counter()"),
  317. deque: lambda _object: ("deque([", "])", "deque()"),
  318. dict: lambda _object: ("{", "}", "{}"),
  319. UserDict: lambda _object: ("{", "}", "{}"),
  320. frozenset: lambda _object: ("frozenset({", "})", "frozenset()"),
  321. list: lambda _object: ("[", "]", "[]"),
  322. UserList: lambda _object: ("[", "]", "[]"),
  323. set: lambda _object: ("{", "}", "set()"),
  324. tuple: lambda _object: ("(", ")", "()"),
  325. MappingProxyType: lambda _object: ("mappingproxy({", "})", "mappingproxy({})"),
  326. }
  327. _CONTAINERS = tuple(_BRACES.keys())
  328. _MAPPING_CONTAINERS = (dict, os._Environ, MappingProxyType, UserDict)
  329. def is_expandable(obj: Any) -> bool:
  330. """Check if an object may be expanded by pretty print."""
  331. return (
  332. _safe_isinstance(obj, _CONTAINERS)
  333. or (is_dataclass(obj))
  334. or (hasattr(obj, "__rich_repr__"))
  335. or _is_attr_object(obj)
  336. ) and not isclass(obj)
  337. @dataclass
  338. class Node:
  339. """A node in a repr tree. May be atomic or a container."""
  340. key_repr: str = ""
  341. value_repr: str = ""
  342. open_brace: str = ""
  343. close_brace: str = ""
  344. empty: str = ""
  345. last: bool = False
  346. is_tuple: bool = False
  347. is_namedtuple: bool = False
  348. children: Optional[List["Node"]] = None
  349. key_separator: str = ": "
  350. separator: str = ", "
  351. def iter_tokens(self) -> Iterable[str]:
  352. """Generate tokens for this node."""
  353. if self.key_repr:
  354. yield self.key_repr
  355. yield self.key_separator
  356. if self.value_repr:
  357. yield self.value_repr
  358. elif self.children is not None:
  359. if self.children:
  360. yield self.open_brace
  361. if self.is_tuple and not self.is_namedtuple and len(self.children) == 1:
  362. yield from self.children[0].iter_tokens()
  363. yield ","
  364. else:
  365. for child in self.children:
  366. yield from child.iter_tokens()
  367. if not child.last:
  368. yield self.separator
  369. yield self.close_brace
  370. else:
  371. yield self.empty
  372. def check_length(self, start_length: int, max_length: int) -> bool:
  373. """Check the length fits within a limit.
  374. Args:
  375. start_length (int): Starting length of the line (indent, prefix, suffix).
  376. max_length (int): Maximum length.
  377. Returns:
  378. bool: True if the node can be rendered within max length, otherwise False.
  379. """
  380. total_length = start_length
  381. for token in self.iter_tokens():
  382. total_length += cell_len(token)
  383. if total_length > max_length:
  384. return False
  385. return True
  386. def __str__(self) -> str:
  387. repr_text = "".join(self.iter_tokens())
  388. return repr_text
  389. def render(
  390. self, max_width: int = 80, indent_size: int = 4, expand_all: bool = False
  391. ) -> str:
  392. """Render the node to a pretty repr.
  393. Args:
  394. max_width (int, optional): Maximum width of the repr. Defaults to 80.
  395. indent_size (int, optional): Size of indents. Defaults to 4.
  396. expand_all (bool, optional): Expand all levels. Defaults to False.
  397. Returns:
  398. str: A repr string of the original object.
  399. """
  400. lines = [_Line(node=self, is_root=True)]
  401. line_no = 0
  402. while line_no < len(lines):
  403. line = lines[line_no]
  404. if line.expandable and not line.expanded:
  405. if expand_all or not line.check_length(max_width):
  406. lines[line_no : line_no + 1] = line.expand(indent_size)
  407. line_no += 1
  408. repr_str = "\n".join(str(line) for line in lines)
  409. return repr_str
  410. @dataclass
  411. class _Line:
  412. """A line in repr output."""
  413. parent: Optional["_Line"] = None
  414. is_root: bool = False
  415. node: Optional[Node] = None
  416. text: str = ""
  417. suffix: str = ""
  418. whitespace: str = ""
  419. expanded: bool = False
  420. last: bool = False
  421. @property
  422. def expandable(self) -> bool:
  423. """Check if the line may be expanded."""
  424. return bool(self.node is not None and self.node.children)
  425. def check_length(self, max_length: int) -> bool:
  426. """Check this line fits within a given number of cells."""
  427. start_length = (
  428. len(self.whitespace) + cell_len(self.text) + cell_len(self.suffix)
  429. )
  430. assert self.node is not None
  431. return self.node.check_length(start_length, max_length)
  432. def expand(self, indent_size: int) -> Iterable["_Line"]:
  433. """Expand this line by adding children on their own line."""
  434. node = self.node
  435. assert node is not None
  436. whitespace = self.whitespace
  437. assert node.children
  438. if node.key_repr:
  439. new_line = yield _Line(
  440. text=f"{node.key_repr}{node.key_separator}{node.open_brace}",
  441. whitespace=whitespace,
  442. )
  443. else:
  444. new_line = yield _Line(text=node.open_brace, whitespace=whitespace)
  445. child_whitespace = self.whitespace + " " * indent_size
  446. tuple_of_one = node.is_tuple and len(node.children) == 1
  447. for last, child in loop_last(node.children):
  448. separator = "," if tuple_of_one else node.separator
  449. line = _Line(
  450. parent=new_line,
  451. node=child,
  452. whitespace=child_whitespace,
  453. suffix=separator,
  454. last=last and not tuple_of_one,
  455. )
  456. yield line
  457. yield _Line(
  458. text=node.close_brace,
  459. whitespace=whitespace,
  460. suffix=self.suffix,
  461. last=self.last,
  462. )
  463. def __str__(self) -> str:
  464. if self.last:
  465. return f"{self.whitespace}{self.text}{self.node or ''}"
  466. else:
  467. return (
  468. f"{self.whitespace}{self.text}{self.node or ''}{self.suffix.rstrip()}"
  469. )
  470. def _is_namedtuple(obj: Any) -> bool:
  471. """Checks if an object is most likely a namedtuple. It is possible
  472. to craft an object that passes this check and isn't a namedtuple, but
  473. there is only a minuscule chance of this happening unintentionally.
  474. Args:
  475. obj (Any): The object to test
  476. Returns:
  477. bool: True if the object is a namedtuple. False otherwise.
  478. """
  479. try:
  480. fields = getattr(obj, "_fields", None)
  481. except Exception:
  482. # Being very defensive - if we cannot get the attr then its not a namedtuple
  483. return False
  484. return isinstance(obj, tuple) and isinstance(fields, tuple)
  485. def traverse(
  486. _object: Any,
  487. max_length: Optional[int] = None,
  488. max_string: Optional[int] = None,
  489. max_depth: Optional[int] = None,
  490. ) -> Node:
  491. """Traverse object and generate a tree.
  492. Args:
  493. _object (Any): Object to be traversed.
  494. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  495. Defaults to None.
  496. max_string (int, optional): Maximum length of string before truncating, or None to disable truncating.
  497. Defaults to None.
  498. max_depth (int, optional): Maximum depth of data structures, or None for no maximum.
  499. Defaults to None.
  500. Returns:
  501. Node: The root of a tree structure which can be used to render a pretty repr.
  502. """
  503. def to_repr(obj: Any) -> str:
  504. """Get repr string for an object, but catch errors."""
  505. if (
  506. max_string is not None
  507. and _safe_isinstance(obj, (bytes, str))
  508. and len(obj) > max_string
  509. ):
  510. truncated = len(obj) - max_string
  511. obj_repr = f"{obj[:max_string]!r}+{truncated}"
  512. else:
  513. try:
  514. obj_repr = repr(obj)
  515. except Exception as error:
  516. obj_repr = f"<repr-error {str(error)!r}>"
  517. return obj_repr
  518. visited_ids: Set[int] = set()
  519. push_visited = visited_ids.add
  520. pop_visited = visited_ids.remove
  521. def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
  522. """Walk the object depth first."""
  523. obj_id = id(obj)
  524. if obj_id in visited_ids:
  525. # Recursion detected
  526. return Node(value_repr="...")
  527. obj_type = type(obj)
  528. children: List[Node]
  529. reached_max_depth = max_depth is not None and depth >= max_depth
  530. def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
  531. for arg in rich_args:
  532. if _safe_isinstance(arg, tuple):
  533. if len(arg) == 3:
  534. key, child, default = arg
  535. if default == child:
  536. continue
  537. yield key, child
  538. elif len(arg) == 2:
  539. key, child = arg
  540. yield key, child
  541. elif len(arg) == 1:
  542. yield arg[0]
  543. else:
  544. yield arg
  545. try:
  546. fake_attributes = hasattr(
  547. obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
  548. )
  549. except Exception:
  550. fake_attributes = False
  551. rich_repr_result: Optional[RichReprResult] = None
  552. if not fake_attributes:
  553. try:
  554. if hasattr(obj, "__rich_repr__") and not isclass(obj):
  555. rich_repr_result = obj.__rich_repr__()
  556. except Exception:
  557. pass
  558. if rich_repr_result is not None:
  559. push_visited(obj_id)
  560. angular = getattr(obj.__rich_repr__, "angular", False)
  561. args = list(iter_rich_args(rich_repr_result))
  562. class_name = obj.__class__.__name__
  563. if args:
  564. children = []
  565. append = children.append
  566. if reached_max_depth:
  567. if angular:
  568. node = Node(value_repr=f"<{class_name}...>")
  569. else:
  570. node = Node(value_repr=f"{class_name}(...)")
  571. else:
  572. if angular:
  573. node = Node(
  574. open_brace=f"<{class_name} ",
  575. close_brace=">",
  576. children=children,
  577. last=root,
  578. separator=" ",
  579. )
  580. else:
  581. node = Node(
  582. open_brace=f"{class_name}(",
  583. close_brace=")",
  584. children=children,
  585. last=root,
  586. )
  587. for last, arg in loop_last(args):
  588. if _safe_isinstance(arg, tuple):
  589. key, child = arg
  590. child_node = _traverse(child, depth=depth + 1)
  591. child_node.last = last
  592. child_node.key_repr = key
  593. child_node.key_separator = "="
  594. append(child_node)
  595. else:
  596. child_node = _traverse(arg, depth=depth + 1)
  597. child_node.last = last
  598. append(child_node)
  599. else:
  600. node = Node(
  601. value_repr=f"<{class_name}>" if angular else f"{class_name}()",
  602. children=[],
  603. last=root,
  604. )
  605. pop_visited(obj_id)
  606. elif _is_attr_object(obj) and not fake_attributes:
  607. push_visited(obj_id)
  608. children = []
  609. append = children.append
  610. attr_fields = _get_attr_fields(obj)
  611. if attr_fields:
  612. if reached_max_depth:
  613. node = Node(value_repr=f"{obj.__class__.__name__}(...)")
  614. else:
  615. node = Node(
  616. open_brace=f"{obj.__class__.__name__}(",
  617. close_brace=")",
  618. children=children,
  619. last=root,
  620. )
  621. def iter_attrs() -> Iterable[
  622. Tuple[str, Any, Optional[Callable[[Any], str]]]
  623. ]:
  624. """Iterate over attr fields and values."""
  625. for attr in attr_fields:
  626. if attr.repr:
  627. try:
  628. value = getattr(obj, attr.name)
  629. except Exception as error:
  630. # Can happen, albeit rarely
  631. yield (attr.name, error, None)
  632. else:
  633. yield (
  634. attr.name,
  635. value,
  636. attr.repr if callable(attr.repr) else None,
  637. )
  638. for last, (name, value, repr_callable) in loop_last(iter_attrs()):
  639. if repr_callable:
  640. child_node = Node(value_repr=str(repr_callable(value)))
  641. else:
  642. child_node = _traverse(value, depth=depth + 1)
  643. child_node.last = last
  644. child_node.key_repr = name
  645. child_node.key_separator = "="
  646. append(child_node)
  647. else:
  648. node = Node(
  649. value_repr=f"{obj.__class__.__name__}()", children=[], last=root
  650. )
  651. pop_visited(obj_id)
  652. elif (
  653. is_dataclass(obj)
  654. and not _safe_isinstance(obj, type)
  655. and not fake_attributes
  656. and _is_dataclass_repr(obj)
  657. ):
  658. push_visited(obj_id)
  659. children = []
  660. append = children.append
  661. if reached_max_depth:
  662. node = Node(value_repr=f"{obj.__class__.__name__}(...)")
  663. else:
  664. node = Node(
  665. open_brace=f"{obj.__class__.__name__}(",
  666. close_brace=")",
  667. children=children,
  668. last=root,
  669. empty=f"{obj.__class__.__name__}()",
  670. )
  671. for last, field in loop_last(
  672. field for field in fields(obj) if field.repr
  673. ):
  674. child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
  675. child_node.key_repr = field.name
  676. child_node.last = last
  677. child_node.key_separator = "="
  678. append(child_node)
  679. pop_visited(obj_id)
  680. elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
  681. push_visited(obj_id)
  682. class_name = obj.__class__.__name__
  683. if reached_max_depth:
  684. # If we've reached the max depth, we still show the class name, but not its contents
  685. node = Node(
  686. value_repr=f"{class_name}(...)",
  687. )
  688. else:
  689. children = []
  690. append = children.append
  691. node = Node(
  692. open_brace=f"{class_name}(",
  693. close_brace=")",
  694. children=children,
  695. empty=f"{class_name}()",
  696. )
  697. for last, (key, value) in loop_last(obj._asdict().items()):
  698. child_node = _traverse(value, depth=depth + 1)
  699. child_node.key_repr = key
  700. child_node.last = last
  701. child_node.key_separator = "="
  702. append(child_node)
  703. pop_visited(obj_id)
  704. elif _safe_isinstance(obj, _CONTAINERS):
  705. for container_type in _CONTAINERS:
  706. if _safe_isinstance(obj, container_type):
  707. obj_type = container_type
  708. break
  709. push_visited(obj_id)
  710. open_brace, close_brace, empty = _BRACES[obj_type](obj)
  711. if reached_max_depth:
  712. node = Node(value_repr=f"{open_brace}...{close_brace}")
  713. elif obj_type.__repr__ != type(obj).__repr__:
  714. node = Node(value_repr=to_repr(obj), last=root)
  715. elif obj:
  716. children = []
  717. node = Node(
  718. open_brace=open_brace,
  719. close_brace=close_brace,
  720. children=children,
  721. last=root,
  722. )
  723. append = children.append
  724. num_items = len(obj)
  725. last_item_index = num_items - 1
  726. if _safe_isinstance(obj, _MAPPING_CONTAINERS):
  727. iter_items = iter(obj.items())
  728. if max_length is not None:
  729. iter_items = islice(iter_items, max_length)
  730. for index, (key, child) in enumerate(iter_items):
  731. child_node = _traverse(child, depth=depth + 1)
  732. child_node.key_repr = to_repr(key)
  733. child_node.last = index == last_item_index
  734. append(child_node)
  735. else:
  736. iter_values = iter(obj)
  737. if max_length is not None:
  738. iter_values = islice(iter_values, max_length)
  739. for index, child in enumerate(iter_values):
  740. child_node = _traverse(child, depth=depth + 1)
  741. child_node.last = index == last_item_index
  742. append(child_node)
  743. if max_length is not None and num_items > max_length:
  744. append(Node(value_repr=f"... +{num_items - max_length}", last=True))
  745. else:
  746. node = Node(empty=empty, children=[], last=root)
  747. pop_visited(obj_id)
  748. else:
  749. node = Node(value_repr=to_repr(obj), last=root)
  750. node.is_tuple = _safe_isinstance(obj, tuple)
  751. node.is_namedtuple = _is_namedtuple(obj)
  752. return node
  753. node = _traverse(_object, root=True)
  754. return node
  755. def pretty_repr(
  756. _object: Any,
  757. *,
  758. max_width: int = 80,
  759. indent_size: int = 4,
  760. max_length: Optional[int] = None,
  761. max_string: Optional[int] = None,
  762. max_depth: Optional[int] = None,
  763. expand_all: bool = False,
  764. ) -> str:
  765. """Prettify repr string by expanding on to new lines to fit within a given width.
  766. Args:
  767. _object (Any): Object to repr.
  768. max_width (int, optional): Desired maximum width of repr string. Defaults to 80.
  769. indent_size (int, optional): Number of spaces to indent. Defaults to 4.
  770. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  771. Defaults to None.
  772. max_string (int, optional): Maximum length of string before truncating, or None to disable truncating.
  773. Defaults to None.
  774. max_depth (int, optional): Maximum depth of nested data structure, or None for no depth.
  775. Defaults to None.
  776. expand_all (bool, optional): Expand all containers regardless of available width. Defaults to False.
  777. Returns:
  778. str: A possibly multi-line representation of the object.
  779. """
  780. if _safe_isinstance(_object, Node):
  781. node = _object
  782. else:
  783. node = traverse(
  784. _object, max_length=max_length, max_string=max_string, max_depth=max_depth
  785. )
  786. repr_str: str = node.render(
  787. max_width=max_width, indent_size=indent_size, expand_all=expand_all
  788. )
  789. return repr_str
  790. def pprint(
  791. _object: Any,
  792. *,
  793. console: Optional["Console"] = None,
  794. indent_guides: bool = True,
  795. max_length: Optional[int] = None,
  796. max_string: Optional[int] = None,
  797. max_depth: Optional[int] = None,
  798. expand_all: bool = False,
  799. ) -> None:
  800. """A convenience function for pretty printing.
  801. Args:
  802. _object (Any): Object to pretty print.
  803. console (Console, optional): Console instance, or None to use default. Defaults to None.
  804. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
  805. Defaults to None.
  806. max_string (int, optional): Maximum length of strings before truncating, or None to disable. Defaults to None.
  807. max_depth (int, optional): Maximum depth for nested data structures, or None for unlimited depth. Defaults to None.
  808. indent_guides (bool, optional): Enable indentation guides. Defaults to True.
  809. expand_all (bool, optional): Expand all containers. Defaults to False.
  810. """
  811. _console = get_console() if console is None else console
  812. _console.print(
  813. Pretty(
  814. _object,
  815. max_length=max_length,
  816. max_string=max_string,
  817. max_depth=max_depth,
  818. indent_guides=indent_guides,
  819. expand_all=expand_all,
  820. overflow="ignore",
  821. ),
  822. soft_wrap=True,
  823. )
  824. if __name__ == "__main__": # pragma: no cover
  825. class BrokenRepr:
  826. def __repr__(self) -> str:
  827. 1 / 0
  828. return "this will fail"
  829. from typing import NamedTuple
  830. class StockKeepingUnit(NamedTuple):
  831. name: str
  832. description: str
  833. price: float
  834. category: str
  835. reviews: List[str]
  836. d = defaultdict(int)
  837. d["foo"] = 5
  838. data = {
  839. "foo": [
  840. 1,
  841. "Hello World!",
  842. 100.123,
  843. 323.232,
  844. 432324.0,
  845. {5, 6, 7, (1, 2, 3, 4), 8},
  846. ],
  847. "bar": frozenset({1, 2, 3}),
  848. "defaultdict": defaultdict(
  849. list, {"crumble": ["apple", "rhubarb", "butter", "sugar", "flour"]}
  850. ),
  851. "counter": Counter(
  852. [
  853. "apple",
  854. "orange",
  855. "pear",
  856. "kumquat",
  857. "kumquat",
  858. "durian" * 100,
  859. ]
  860. ),
  861. "atomic": (False, True, None),
  862. "namedtuple": StockKeepingUnit(
  863. "Sparkling British Spring Water",
  864. "Carbonated spring water",
  865. 0.9,
  866. "water",
  867. ["its amazing!", "its terrible!"],
  868. ),
  869. "Broken": BrokenRepr(),
  870. }
  871. data["foo"].append(data) # type: ignore[attr-defined]
  872. from pip._vendor.rich import print
  873. # print(Pretty(data, indent_guides=True, max_string=20))
  874. class Thing:
  875. def __repr__(self) -> str:
  876. return "Hello\x1b[38;5;239m World!"
  877. print(Pretty(Thing()))