mirror of
https://github.com/Textualize/rich.git
synced 2025-08-03 17:48:25 +00:00
test fake attributes
This commit is contained in:
parent
ce512856dd
commit
f934b09ecb
2 changed files with 22 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
import builtins
|
||||
import os
|
||||
from rich.repr import RichReprResult
|
||||
import sys
|
||||
from array import array
|
||||
from collections import Counter, defaultdict, deque, UserDict, UserList
|
||||
|
@ -503,9 +504,16 @@ def traverse(
|
|||
else:
|
||||
yield arg
|
||||
|
||||
if hasattr(obj, "__rich_repr__") and not isclass(obj):
|
||||
rich_repr_result: Optional[RichReprResult] = None
|
||||
try:
|
||||
if hasattr(obj, "__rich_repr__") and not isclass(obj):
|
||||
rich_repr_result: Optional[RichReprResult] = obj.__rich_repr__()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if rich_repr_result is not None:
|
||||
angular = getattr(obj.__rich_repr__, "angular", False)
|
||||
args = list(iter_rich_args(obj.__rich_repr__()))
|
||||
args = list(iter_rich_args(rich_repr_result))
|
||||
class_name = obj.__class__.__name__
|
||||
|
||||
if args:
|
||||
|
|
|
@ -250,3 +250,15 @@ def test_user_dict():
|
|||
result = pretty_repr(d2, expand_all=True)
|
||||
print(repr(result))
|
||||
assert result == "FOO"
|
||||
|
||||
|
||||
def test_lying_attribute():
|
||||
"""Test getattr doesn't break rich repr protocol"""
|
||||
|
||||
class Foo:
|
||||
def __getattr__(self, attr):
|
||||
return "foo"
|
||||
|
||||
foo = Foo()
|
||||
result = pretty_repr(foo)
|
||||
assert "Foo" in result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue