Skip to content

gh-127750: Improve repr of functools.singledispatchmethod #130220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,15 @@ def __get__(self, obj, cls=None):
def __isabstractmethod__(self):
return getattr(self.func, '__isabstractmethod__', False)

def __repr__(self):
try:
name = self.func.__qualname__
except AttributeError:
try:
name = self.func.__name__
except AttributeError:
name = '?'
return f'<single dispatch method descriptor {name}>'

class _singledispatchmethod_get:
def __init__(self, unbound, obj, cls):
Expand All @@ -1059,6 +1068,19 @@ def __init__(self, unbound, obj, cls):
except AttributeError:
pass

def __repr__(self):
try:
name = self.__qualname__
except AttributeError:
try:
name = self.__name__
except AttributeError:
name = '?'
if self._obj is not None:
return f'<bound single dispatch method {name} of {self._obj!r}>'
else:
return f'<single dispatch method {name}>'

def __call__(self, /, *args, **kwargs):
if not args:
funcname = getattr(self._unbound.func, '__name__',
Expand Down
61 changes: 61 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,6 +2936,67 @@ def static_func(arg: int) -> str:
self.assertEqual(A.static_func.__name__, 'static_func')
self.assertEqual(A().static_func.__name__, 'static_func')

def test_method_repr(self):
class Callable:
def __call__(self, *args):
pass

class CallableWithName:
__name__ = 'NOQUALNAME'
def __call__(self, *args):
pass

class A:
@functools.singledispatchmethod
def func(self, arg):
pass
@functools.singledispatchmethod
@classmethod
def cls_func(cls, arg):
pass
@functools.singledispatchmethod
@staticmethod
def static_func(arg):
pass
# No __qualname__, only __name__
no_qualname = functools.singledispatchmethod(CallableWithName())
# No __qualname__, no __name__
no_name = functools.singledispatchmethod(Callable())

self.assertEqual(repr(A.__dict__['func']),
f'<single dispatch method descriptor {A.__qualname__}.func>')
self.assertEqual(repr(A.__dict__['cls_func']),
f'<single dispatch method descriptor {A.__qualname__}.cls_func>')
self.assertEqual(repr(A.__dict__['static_func']),
f'<single dispatch method descriptor {A.__qualname__}.static_func>')
self.assertEqual(repr(A.__dict__['no_qualname']),
f'<single dispatch method descriptor NOQUALNAME>')
self.assertEqual(repr(A.__dict__['no_name']),
f'<single dispatch method descriptor ?>')

self.assertEqual(repr(A.func),
f'<single dispatch method {A.__qualname__}.func>')
self.assertEqual(repr(A.cls_func),
f'<single dispatch method {A.__qualname__}.cls_func>')
self.assertEqual(repr(A.static_func),
f'<single dispatch method {A.__qualname__}.static_func>')
self.assertEqual(repr(A.no_qualname),
f'<single dispatch method NOQUALNAME>')
self.assertEqual(repr(A.no_name),
f'<single dispatch method ?>')

a = A()
self.assertEqual(repr(a.func),
f'<bound single dispatch method {A.__qualname__}.func of {a!r}>')
self.assertEqual(repr(a.cls_func),
f'<bound single dispatch method {A.__qualname__}.cls_func of {a!r}>')
self.assertEqual(repr(a.static_func),
f'<bound single dispatch method {A.__qualname__}.static_func of {a!r}>')
self.assertEqual(repr(a.no_qualname),
f'<bound single dispatch method NOQUALNAME of {a!r}>')
self.assertEqual(repr(a.no_name),
f'<bound single dispatch method ? of {a!r}>')

def test_double_wrapped_methods(self):
def classmethod_friendly_decorator(func):
wrapped = func.__func__
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve repr of :class:`functools.singledispatchmethod` methods and
descriptors.
Loading