mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
Testing pretty.py - objects similar to namedtuples, namedtuple max_depth
This commit is contained in:
parent
a95c1f1c48
commit
934c6ef720
2 changed files with 24 additions and 18 deletions
|
@ -744,12 +744,6 @@ def traverse(
|
|||
|
||||
pop_visited(obj_id)
|
||||
elif _is_namedtuple(obj):
|
||||
obj_id = id(obj)
|
||||
if obj_id in visited_ids:
|
||||
# Recursion detected
|
||||
return Node(value_repr="...")
|
||||
push_visited(obj_id)
|
||||
|
||||
children = []
|
||||
append = children.append
|
||||
if reached_max_depth:
|
||||
|
@ -760,15 +754,12 @@ def traverse(
|
|||
close_brace=")",
|
||||
children=children,
|
||||
)
|
||||
|
||||
for last, (key, value) in loop_last(obj._asdict().items()):
|
||||
child_node = _traverse(value, depth=depth + 1)
|
||||
child_node.key_repr = key
|
||||
child_node.last = last
|
||||
child_node.key_separator = "="
|
||||
append(child_node)
|
||||
|
||||
pop_visited(obj_id)
|
||||
elif _safe_isinstance(obj, _CONTAINERS):
|
||||
for container_type in _CONTAINERS:
|
||||
if _safe_isinstance(obj, container_type):
|
||||
|
|
|
@ -3,7 +3,7 @@ import sys
|
|||
from array import array
|
||||
from collections import UserDict, defaultdict
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
from typing import List, NamedTuple
|
||||
|
||||
import attr
|
||||
import pytest
|
||||
|
@ -169,17 +169,17 @@ def test_pretty_dataclass():
|
|||
assert result == "ExampleDataclass(foo=1000, bar=..., baz=['foo', 'bar', 'baz'])"
|
||||
|
||||
|
||||
class StockKeepingUnit(NamedTuple):
|
||||
name: str
|
||||
description: str
|
||||
price: float
|
||||
category: str
|
||||
reviews: List[str]
|
||||
|
||||
|
||||
def test_pretty_namedtuple():
|
||||
console = Console(color_system=None)
|
||||
console.begin_capture()
|
||||
from typing import NamedTuple
|
||||
|
||||
class StockKeepingUnit(NamedTuple):
|
||||
name: str
|
||||
description: str
|
||||
price: float
|
||||
category: str
|
||||
reviews: List[str]
|
||||
|
||||
example_namedtuple = StockKeepingUnit(
|
||||
"Sparkling British Spring Water",
|
||||
|
@ -204,6 +204,21 @@ def test_pretty_namedtuple():
|
|||
)
|
||||
|
||||
|
||||
def test_pretty_namedtuple_fields_invalid_type():
|
||||
class LooksLikeANamedTupleButIsnt(tuple):
|
||||
_fields = "blah"
|
||||
|
||||
instance = LooksLikeANamedTupleButIsnt()
|
||||
result = pretty_repr(instance)
|
||||
assert result == "()" # Treated as tuple
|
||||
|
||||
|
||||
def test_pretty_namedtuple_max_depth():
|
||||
instance = {"unit": StockKeepingUnit("a", "b", 1.0, "c", ["d", "e"])}
|
||||
result = pretty_repr(instance, max_depth=1)
|
||||
assert result == "{'unit': ...}"
|
||||
|
||||
|
||||
def test_small_width():
|
||||
test = ["Hello world! 12345"]
|
||||
result = pretty_repr(test, max_width=10)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue