Skip to content

Commit 2693269

Browse files
author
Farbod Ahmadian
committed
refactor: simplify bound method representation
1 parent f426c0b commit 2693269

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/_pytest/_io/saferepr.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pprint
22
import reprlib
3+
from types import MethodType
34
from typing import Optional
45

56

@@ -58,7 +59,17 @@ def repr(self, x: object) -> str:
5859
if self.use_ascii:
5960
s = ascii(x)
6061
else:
61-
s = super().repr(x)
62+
if isinstance(x, MethodType):
63+
# for bound methods, skip redundant <bound method ...> information
64+
s = x.__name__
65+
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)
6273

6374
except (KeyboardInterrupt, SystemExit):
6475
raise

0 commit comments

Comments
 (0)