test fake attributes

This commit is contained in:
Will McGugan 2021-09-18 09:43:09 +01:00
parent ce512856dd
commit f934b09ecb
2 changed files with 22 additions and 2 deletions

View file

@ -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:

View file

@ -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