From fe2f2067a1a9db19b675f2bdf1f42db52f426a63 Mon Sep 17 00:00:00 2001 From: Farbod Ahmadian Date: Wed, 19 Jun 2024 22:45:07 +0200 Subject: [PATCH] refactor: simplify bound method representation --- src/_pytest/_io/saferepr.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index 5ace41822..49f747dc2 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -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 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