Skip to content

Commit

Permalink
pythongh-121025: Improve partialmethod.__repr__ (pythonGH-121033)
Browse files Browse the repository at this point in the history
It no longer contains redundant commas and spaces.
  • Loading branch information
picnixz authored and estyxx committed Jul 17, 2024
1 parent 3b79259 commit 920770e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
16 changes: 7 additions & 9 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,13 @@ def __init__(self, func, /, *args, **keywords):
self.keywords = keywords

def __repr__(self):
args = ", ".join(map(repr, self.args))
keywords = ", ".join("{}={!r}".format(k, v)
for k, v in self.keywords.items())
format_string = "{module}.{cls}({func}, {args}, {keywords})"
return format_string.format(module=self.__class__.__module__,
cls=self.__class__.__qualname__,
func=self.func,
args=args,
keywords=keywords)
cls = type(self)
module = cls.__module__
qualname = cls.__qualname__
args = [repr(self.func)]
args.extend(map(repr, self.args))
args.extend(f"{k}={v!r}" for k, v in self.keywords.items())
return f"{module}.{qualname}({', '.join(args)})"

def _make_unbound_method(self):
def _method(cls_or_self, /, *args, **keywords):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ def test_handle_repr(self):
h = asyncio.Handle(cb, (), self.loop)

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

Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,14 @@ class B:
method = functools.partialmethod(func=capture, a=1)

def test_repr(self):
self.assertEqual(repr(vars(self.A)['nothing']),
'functools.partialmethod({})'.format(capture))
self.assertEqual(repr(vars(self.A)['positional']),
'functools.partialmethod({}, 1)'.format(capture))
self.assertEqual(repr(vars(self.A)['keywords']),
'functools.partialmethod({}, a=2)'.format(capture))
self.assertEqual(repr(vars(self.A)['spec_keywords']),
'functools.partialmethod({}, self=1, func=2)'.format(capture))
self.assertEqual(repr(vars(self.A)['both']),
'functools.partialmethod({}, 3, b=4)'.format(capture))

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

0 comments on commit 920770e

Please sign in to comment.