Walk the object depth first.
(obj: Any, root: bool = False, depth: int = 0)
| 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}(...)") |
no test coverage detected