gh-127750: Improve repr of functools.singledispatchmethod (GH-130220)

This commit is contained in:
Serhiy Storchaka 2025-03-05 13:10:05 +02:00 committed by GitHub
parent 67a942d427
commit f33d21e24f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 85 additions and 0 deletions

View file

@ -1033,6 +1033,15 @@ class singledispatchmethod:
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):
@ -1052,6 +1061,19 @@ class _singledispatchmethod_get:
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__',