refactor: simplify bound method representation
This commit is contained in:
parent
9947ec3ad1
commit
fe2f2067a1
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import pprint
|
import pprint
|
||||||
import reprlib
|
import reprlib
|
||||||
|
from types import MethodType
|
||||||
|
|
||||||
|
|
||||||
def _try_repr_or_str(obj: object) -> str:
|
def _try_repr_or_str(obj: object) -> str:
|
||||||
|
@ -58,6 +59,16 @@ class SafeRepr(reprlib.Repr):
|
||||||
try:
|
try:
|
||||||
if self.use_ascii:
|
if self.use_ascii:
|
||||||
s = ascii(x)
|
s = ascii(x)
|
||||||
|
else:
|
||||||
|
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:
|
else:
|
||||||
s = super().repr(x)
|
s = super().repr(x)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue