]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-121025: Improve partialmethod.__repr__ (GH-121033) (#121037)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 26 Jun 2024 09:49:42 +0000 (11:49 +0200)
committerGitHub <noreply@github.com>
Wed, 26 Jun 2024 09:49:42 +0000 (09:49 +0000)
gh-121025: Improve partialmethod.__repr__ (GH-121033)

It no longer contains redundant commas and spaces.
(cherry picked from commit d2646e3f45e3e4e831ee2ae84d55b161a361d592)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/functools.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2024-06-26-10-13-40.gh-issue-121025.M-XXlV.rst [new file with mode: 0644]

index a80e1a6c6a56acc0f41853dfe824a23b2293adf8..3d0fd6671fb63e1965f24399e63bdff95ceb1969 100644 (file)
@@ -373,15 +373,13 @@ class partialmethod(object):
             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):
index 88c85a36b5d448dd21a7be941ca567a5340493c2..c92bc81618c98956f41bcb8b46f6f002a4d023fa 100644 (file)
@@ -2364,7 +2364,7 @@ class HandleTests(test_utils.TestCase):
         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)
 
index 26701ea8b4daf9445e2e1fd8d4a40c6aa5a4a2d0..559213fef1313d9ee169c49d1bbff0aa4fa5977b 100644 (file)
@@ -569,6 +569,14 @@ class TestPartialMethod(unittest.TestCase):
                 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))
 
diff --git a/Misc/NEWS.d/next/Library/2024-06-26-10-13-40.gh-issue-121025.M-XXlV.rst b/Misc/NEWS.d/next/Library/2024-06-26-10-13-40.gh-issue-121025.M-XXlV.rst
new file mode 100644 (file)
index 0000000..38cad61
--- /dev/null
@@ -0,0 +1,2 @@
+Improve the :meth:`~object.__repr__` of :class:`functools.partialmethod`.
+Patch by Bénédikt Tran.