refactor: simplify bound method representation

This commit is contained in:
Farbod Ahmadian 2024-06-19 22:45:07 +02:00 committed by Farbod Ahmadian
parent 9947ec3ad1
commit fe2f2067a1
1 changed files with 12 additions and 1 deletions

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import pprint
import reprlib
from types import MethodType
def _try_repr_or_str(obj: object) -> str:
@ -59,7 +60,17 @@ class SafeRepr(reprlib.Repr):
if self.use_ascii:
s = ascii(x)
else:
s = super().repr(x)
if isinstance(x, MethodType):
# 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)
except (KeyboardInterrupt, SystemExit):
raise