tests: add for bound method representation

This commit is contained in:
Farbod Ahmadian 2024-06-20 11:01:56 +02:00 committed by Farbod Ahmadian
parent fe2f2067a1
commit c6d1b0b17f
2 changed files with 26 additions and 7 deletions

View File

@ -64,13 +64,7 @@ class SafeRepr(reprlib.Repr):
# for bound methods, skip redundant <bound method ...> information
s = x.__name__
else:
# if none of the mro classes have implemented __repr__
# show class name
mro_classes = x.__class__.mro()[:-1]
if not any("__repr__" in cls.__dict__ for cls in mro_classes):
s = x.__class__.__name__
else:
s = super().repr(x)
s = super().repr(x)
except (KeyboardInterrupt, SystemExit):
raise

View File

@ -192,3 +192,28 @@ def test_saferepr_unlimited_exc():
assert saferepr_unlimited(A()).startswith(
"<[ValueError(42) raised in repr()] A object at 0x"
)
class TestSafereprUnbounded:
class Help:
def __init__(self, i):
self.i = i
def bound_method(self):
return self.i
def test_saferepr_bound_method(self):
"""saferepr() of a bound method should show only the method name"""
assert saferepr(self.Help(10).bound_method) == "bound_method"
def test_saferepr_unbounded(self):
"""saferepr() of an unbound method should still show the full information"""
obj = self.Help(10)
assert (
saferepr(obj)
== f"<test_saferepr.{self.__class__.__name__}.Help object at 0x{id(obj):x}>"
)
assert (
saferepr(self.Help)
== f"<class 'test_saferepr.{self.__class__.__name__}.Help'>"
)