[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 d2646e3f45)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-06-26 11:49:42 +02:00 committed by GitHub
parent 571cefd8c7
commit 84634254fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 10 deletions

View File

@ -373,15 +373,13 @@ class partialmethod(object):
self.keywords = keywords self.keywords = keywords
def __repr__(self): def __repr__(self):
args = ", ".join(map(repr, self.args)) cls = type(self)
keywords = ", ".join("{}={!r}".format(k, v) module = cls.__module__
for k, v in self.keywords.items()) qualname = cls.__qualname__
format_string = "{module}.{cls}({func}, {args}, {keywords})" args = [repr(self.func)]
return format_string.format(module=self.__class__.__module__, args.extend(map(repr, self.args))
cls=self.__class__.__qualname__, args.extend(f"{k}={v!r}" for k, v in self.keywords.items())
func=self.func, return f"{module}.{qualname}({', '.join(args)})"
args=args,
keywords=keywords)
def _make_unbound_method(self): def _make_unbound_method(self):
def _method(cls_or_self, /, *args, **keywords): def _method(cls_or_self, /, *args, **keywords):

View File

@ -2364,7 +2364,7 @@ class HandleTests(test_utils.TestCase):
h = asyncio.Handle(cb, (), self.loop) h = asyncio.Handle(cb, (), self.loop)
cb_regex = r'<function HandleTests.test_handle_repr .*>' 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}>$' regex = fr'^<Handle {cb_regex} at {re.escape(filename)}:{lineno}>$'
self.assertRegex(repr(h), regex) self.assertRegex(repr(h), regex)

View File

@ -569,6 +569,14 @@ class TestPartialMethod(unittest.TestCase):
method = functools.partialmethod(func=capture, a=1) method = functools.partialmethod(func=capture, a=1)
def test_repr(self): 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']), self.assertEqual(repr(vars(self.A)['both']),
'functools.partialmethod({}, 3, b=4)'.format(capture)) 'functools.partialmethod({}, 3, b=4)'.format(capture))

View File

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