pretty.py 32 KB

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