MCPcopy
hub / github.com/Textualize/rich / _traverse

Function _traverse

rich/pretty.py:621–872  ·  view source on GitHub ↗

Walk the object depth first.

(obj: Any, root: bool = False, depth: int = 0)

Source from the content-addressed store, hash-verified

619 pop_visited = visited_ids.remove
620
621 def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
622 """Walk the object depth first."""
623
624 obj_id = id(obj)
625 if obj_id in visited_ids:
626 # Recursion detected
627 return Node(value_repr="...")
628
629 obj_type = type(obj)
630 children: List[Node]
631 reached_max_depth = max_depth is not None and depth >= max_depth
632
633 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
634 for arg in rich_args:
635 if _safe_isinstance(arg, tuple):
636 if len(arg) == 3:
637 key, child, default = arg
638 if default == child:
639 continue
640 yield key, child
641 elif len(arg) == 2:
642 key, child = arg
643 yield key, child
644 elif len(arg) == 1:
645 yield arg[0]
646 else:
647 yield arg
648
649 try:
650 fake_attributes = hasattr(
651 obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
652 )
653 except Exception:
654 fake_attributes = False
655
656 rich_repr_result: Optional[RichReprResult] = None
657 if not fake_attributes:
658 try:
659 if hasattr(obj, "__rich_repr__") and not isclass(obj):
660 rich_repr_result = obj.__rich_repr__()
661 except Exception:
662 pass
663
664 if rich_repr_result is not None:
665 push_visited(obj_id)
666 angular = getattr(obj.__rich_repr__, "angular", False)
667 args = list(iter_rich_args(rich_repr_result))
668 class_name = obj.__class__.__name__
669
670 if args:
671 children = []
672 append = children.append
673
674 if reached_max_depth:
675 if angular:
676 node = Node(value_repr=f"<{class_name}...>")
677 else:
678 node = Node(value_repr=f"{class_name}(...)")

Callers 1

traverseFunction · 0.85

Calls 12

NodeClass · 0.85
iter_rich_argsFunction · 0.85
loop_lastFunction · 0.85
_safe_isinstanceFunction · 0.85
_is_attr_objectFunction · 0.85
_get_attr_fieldsFunction · 0.85
iter_attrsFunction · 0.85
_is_dataclass_reprFunction · 0.85
_is_namedtupleFunction · 0.85
to_reprFunction · 0.85
__rich_repr__Method · 0.45

Tested by

no test coverage detected