Skip to content

Commit 66eb603

Browse files
gh-127750: Improve repr of functools.singledispatchmethod
1 parent 395335d commit 66eb603

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Lib/functools.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,15 @@ def __get__(self, obj, cls=None):
10401040
def __isabstractmethod__(self):
10411041
return getattr(self.func, '__isabstractmethod__', False)
10421042

1043+
def __repr__(self):
1044+
try:
1045+
name = self.func.__qualname__
1046+
except AttributeError:
1047+
try:
1048+
name = self.func.__name__
1049+
except AttributeError:
1050+
name = '?'
1051+
return f'<single dispatch method descriptor {name}>'
10431052

10441053
class _singledispatchmethod_get:
10451054
def __init__(self, unbound, obj, cls):
@@ -1059,6 +1068,19 @@ def __init__(self, unbound, obj, cls):
10591068
except AttributeError:
10601069
pass
10611070

1071+
def __repr__(self):
1072+
try:
1073+
name = self.__qualname__
1074+
except AttributeError:
1075+
try:
1076+
name = self.__name__
1077+
except AttributeError:
1078+
name = '?'
1079+
if self._obj is not None:
1080+
return f'<bound single dispatch method {name} of {self._obj!r}>'
1081+
else:
1082+
return f'<single dispatch method {name}>'
1083+
10621084
def __call__(self, /, *args, **kwargs):
10631085
if not args:
10641086
funcname = getattr(self._unbound.func, '__name__',

Lib/test/test_functools.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,67 @@ def static_func(arg: int) -> str:
29362936
self.assertEqual(A.static_func.__name__, 'static_func')
29372937
self.assertEqual(A().static_func.__name__, 'static_func')
29382938

2939+
def test_method_repr(self):
2940+
class Callable:
2941+
def __call__(self, *args):
2942+
pass
2943+
2944+
class CallableWithName:
2945+
__name__ = 'NOQUALNAME'
2946+
def __call__(self, *args):
2947+
pass
2948+
2949+
class A:
2950+
@functools.singledispatchmethod
2951+
def func(self, arg):
2952+
pass
2953+
@functools.singledispatchmethod
2954+
@classmethod
2955+
def cls_func(cls, arg):
2956+
pass
2957+
@functools.singledispatchmethod
2958+
@staticmethod
2959+
def static_func(arg):
2960+
pass
2961+
# No __qualname__, only __name__
2962+
no_qualname = functools.singledispatchmethod(CallableWithName())
2963+
# No __qualname__, no __name__
2964+
no_name = functools.singledispatchmethod(Callable())
2965+
2966+
self.assertEqual(repr(A.__dict__['func']),
2967+
f'<single dispatch method descriptor {A.__qualname__}.func>')
2968+
self.assertEqual(repr(A.__dict__['cls_func']),
2969+
f'<single dispatch method descriptor {A.__qualname__}.cls_func>')
2970+
self.assertEqual(repr(A.__dict__['static_func']),
2971+
f'<single dispatch method descriptor {A.__qualname__}.static_func>')
2972+
self.assertEqual(repr(A.__dict__['no_qualname']),
2973+
f'<single dispatch method descriptor NOQUALNAME>')
2974+
self.assertEqual(repr(A.__dict__['no_name']),
2975+
f'<single dispatch method descriptor ?>')
2976+
2977+
self.assertEqual(repr(A.func),
2978+
f'<single dispatch method {A.__qualname__}.func>')
2979+
self.assertEqual(repr(A.cls_func),
2980+
f'<single dispatch method {A.__qualname__}.cls_func>')
2981+
self.assertEqual(repr(A.static_func),
2982+
f'<single dispatch method {A.__qualname__}.static_func>')
2983+
self.assertEqual(repr(A.no_qualname),
2984+
f'<single dispatch method NOQUALNAME>')
2985+
self.assertEqual(repr(A.no_name),
2986+
f'<single dispatch method ?>')
2987+
2988+
a = A()
2989+
self.assertEqual(repr(a.func),
2990+
f'<bound single dispatch method {A.__qualname__}.func of {a!r}>')
2991+
self.assertEqual(repr(a.cls_func),
2992+
f'<bound single dispatch method {A.__qualname__}.cls_func of {a!r}>')
2993+
self.assertEqual(repr(a.static_func),
2994+
f'<bound single dispatch method {A.__qualname__}.static_func of {a!r}>')
2995+
self.assertEqual(repr(a.no_qualname),
2996+
f'<bound single dispatch method NOQUALNAME of {a!r}>')
2997+
self.assertEqual(repr(a.no_name),
2998+
f'<bound single dispatch method ? of {a!r}>')
2999+
29393000
def test_double_wrapped_methods(self):
29403001
def classmethod_friendly_decorator(func):
29413002
wrapped = func.__func__
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve repr of :class:`functools.singledispatchmethod` methods and
2+
descriptors.

0 commit comments

Comments
 (0)