Skip to content

Commit 15712a5

Browse files
author
Farbod Ahmadian
committed
tests: add for bound method representation
1 parent 2693269 commit 15712a5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/_pytest/_io/saferepr.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ def repr(self, x: object) -> str:
6363
# for bound methods, skip redundant <bound method ...> information
6464
s = x.__name__
6565
else:
66-
# if none of the mro classes have implemented __repr__
67-
# show class name
68-
mro_classes = x.__class__.mro()[:-1]
69-
if not any("__repr__" in cls.__dict__ for cls in mro_classes):
70-
s = x.__class__.__name__
71-
else:
72-
s = super().repr(x)
66+
s = super().repr(x)
7367

7468
except (KeyboardInterrupt, SystemExit):
7569
raise

testing/io/test_saferepr.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,28 @@ def __repr__(self):
190190
assert saferepr_unlimited(A()).startswith(
191191
"<[ValueError(42) raised in repr()] A object at 0x"
192192
)
193+
194+
195+
class TestSafereprUnbounded:
196+
class Help:
197+
def __init__(self, i):
198+
self.i = i
199+
200+
def bound_method(self):
201+
return self.i
202+
203+
def test_saferepr_bound_method(self):
204+
"""saferepr() of a bound method should show only the method name"""
205+
assert saferepr(self.Help(10).bound_method) == "bound_method"
206+
207+
def test_saferepr_unbounded(self):
208+
"""saferepr() of an unbound method should still show the full information"""
209+
obj = self.Help(10)
210+
assert (
211+
saferepr(obj)
212+
== f"<test_saferepr.{self.__class__.__name__}.Help object at 0x{id(obj):x}>"
213+
)
214+
assert (
215+
saferepr(self.Help)
216+
== f"<class 'test_saferepr.{self.__class__.__name__}.Help'>"
217+
)

0 commit comments

Comments
 (0)