Skip to content

Commit d2646e3

Browse files
authored
gh-121025: Improve partialmethod.__repr__ (GH-121033)
It no longer contains redundant commas and spaces.
1 parent d8f8243 commit d2646e3

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

Lib/functools.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,13 @@ def __init__(self, func, /, *args, **keywords):
373373
self.keywords = keywords
374374

375375
def __repr__(self):
376-
args = ", ".join(map(repr, self.args))
377-
keywords = ", ".join("{}={!r}".format(k, v)
378-
for k, v in self.keywords.items())
379-
format_string = "{module}.{cls}({func}, {args}, {keywords})"
380-
return format_string.format(module=self.__class__.__module__,
381-
cls=self.__class__.__qualname__,
382-
func=self.func,
383-
args=args,
384-
keywords=keywords)
376+
cls = type(self)
377+
module = cls.__module__
378+
qualname = cls.__qualname__
379+
args = [repr(self.func)]
380+
args.extend(map(repr, self.args))
381+
args.extend(f"{k}={v!r}" for k, v in self.keywords.items())
382+
return f"{module}.{qualname}({', '.join(args)})"
385383

386384
def _make_unbound_method(self):
387385
def _method(cls_or_self, /, *args, **keywords):

Lib/test/test_asyncio/test_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,7 @@ def test_handle_repr(self):
23482348
h = asyncio.Handle(cb, (), self.loop)
23492349

23502350
cb_regex = r'<function HandleTests.test_handle_repr .*>'
2351-
cb_regex = fr'functools.partialmethod\({cb_regex}, , \)\(\)'
2351+
cb_regex = fr'functools.partialmethod\({cb_regex}\)\(\)'
23522352
regex = fr'^<Handle {cb_regex} at {re.escape(filename)}:{lineno}>$'
23532353
self.assertRegex(repr(h), regex)
23542354

Lib/test/test_functools.py

+8
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,14 @@ class B:
569569
method = functools.partialmethod(func=capture, a=1)
570570

571571
def test_repr(self):
572+
self.assertEqual(repr(vars(self.A)['nothing']),
573+
'functools.partialmethod({})'.format(capture))
574+
self.assertEqual(repr(vars(self.A)['positional']),
575+
'functools.partialmethod({}, 1)'.format(capture))
576+
self.assertEqual(repr(vars(self.A)['keywords']),
577+
'functools.partialmethod({}, a=2)'.format(capture))
578+
self.assertEqual(repr(vars(self.A)['spec_keywords']),
579+
'functools.partialmethod({}, self=1, func=2)'.format(capture))
572580
self.assertEqual(repr(vars(self.A)['both']),
573581
'functools.partialmethod({}, 3, b=4)'.format(capture))
574582

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the :meth:`~object.__repr__` of :class:`functools.partialmethod`.
2+
Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)