Skip to content

Commit 41e1ac6

Browse files
[3.12] gh-121025: Improve partialmethod.__repr__ (GH-121033) (GH-121038)
It no longer contains redundant commas and spaces. (cherry picked from commit d2646e3) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 5290e40 commit 41e1ac6

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

Lib/functools.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,13 @@ def __init__(self, func, /, *args, **keywords):
372372
self.keywords = keywords
373373

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

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

Lib/test/test_asyncio/test_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,7 @@ def test_handle_repr(self):
23512351
h = asyncio.Handle(cb, (), self.loop)
23522352

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

Lib/test/test_functools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,14 @@ class B:
564564
method = functools.partialmethod(func=capture, a=1)
565565

566566
def test_repr(self):
567+
self.assertEqual(repr(vars(self.A)['nothing']),
568+
'functools.partialmethod({})'.format(capture))
569+
self.assertEqual(repr(vars(self.A)['positional']),
570+
'functools.partialmethod({}, 1)'.format(capture))
571+
self.assertEqual(repr(vars(self.A)['keywords']),
572+
'functools.partialmethod({}, a=2)'.format(capture))
573+
self.assertEqual(repr(vars(self.A)['spec_keywords']),
574+
'functools.partialmethod({}, self=1, func=2)'.format(capture))
567575
self.assertEqual(repr(vars(self.A)['both']),
568576
'functools.partialmethod({}, 3, b=4)'.format(capture))
569577

Lines changed: 2 additions & 0 deletions
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)