8000 [3.13] gh-121025: Improve partialmethod.__repr__ (GH-121033) (#121037) · python/cpython@8463425 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8463425

Browse files
[3.13] gh-121025: Improve partialmethod.__repr__ (GH-121033) (#121037)
gh-121025: Improve partialmethod.__repr__ (GH-121033) It no longer contains redundant commas and spaces. (cherry picked from commit d2646e3) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 571cefd commit 8463425

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
@@ -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

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

23662366
cb_regex = r'<function HandleTests.test_handle_repr .*>'
2367-
cb_regex = fr'functools.partialmethod\({cb_regex}, , \)\(\)'
2367+
cb_regex = fr'functools.partialmethod\({cb_regex}\)\(\)'
23682368
regex = fr'^<Handle {cb_regex} at {re.escape(filename)}:{lineno}>$'
23692369
self.assertRegex(repr(h), regex)
23702370

Lib/test/test_functools.py

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

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)
0