aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/functools.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2025-03-05 13:10:05 +0200
committerGitHub <noreply@github.com>2025-03-05 13:10:05 +0200
commitf33d21e24fdb05da7512c2a203467c3ffd0e7713 (patch)
tree28eaa07a6a698026bd79eda50d7e6277ded06e86 /Lib/functools.py
parent67a942d4272145ccdbdf4ceff31318e176f71355 (diff)
downloadcpython-f33d21e24fdb05da7512c2a203467c3ffd0e7713.tar.gz
cpython-f33d21e24fdb05da7512c2a203467c3ffd0e7713.zip
gh-127750: Improve repr of functools.singledispatchmethod (GH-130220)
Diffstat (limited to 'Lib/functools.py')
-rw-r--r--Lib/functools.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index 5e2579f6d8e..e0e45bc336c 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -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__',