Skip to content

Commit

Permalink
tests: add for bound method representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Farbod Ahmadian committed Jun 20, 2024
1 parent 2693269 commit 15712a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/_pytest/_io/saferepr.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ def repr(self, x: object) -> str:
# 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
Expand Down
25 changes: 25 additions & 0 deletions testing/io/test_saferepr.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,28 @@ def __repr__(self):
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'>"
)

0 comments on commit 15712a5

Please sign in to comment.