]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127537: Add __class_getitem__ to the python implementation of functools.partial...
authorCF Bolz-Tereick <cfbolz@gmx.de>
Fri, 27 Dec 2024 01:03:47 +0000 (02:03 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Dec 2024 01:03:47 +0000 (17:03 -0800)
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst [new file with mode: 0644]

index eff6540c7f606e081b17e2aff7bb889380a9f19e..786b8aedfd77f5cfcc0e9b667fd2dfe50f3b210a 100644 (file)
@@ -433,6 +433,9 @@ class partial:
         self._phcount = phcount
         self._merger = merger
 
+    __class_getitem__ = classmethod(GenericAlias)
+
+
 try:
     from _functools import partial, Placeholder, _PlaceholderType
 except ImportError:
index ffd2adb8665b4514c8e513a694ba516cbb19d8ef..4a0252cb637a52de77677352ab9d14867a715caa 100644 (file)
@@ -473,6 +473,12 @@ class TestPartial:
         self.assertEqual(a.cmeth(3, b=4), ((1, A, 3), {'a': 2, 'b': 4}))
         self.assertEqual(a.smeth(3, b=4), ((1, 3), {'a': 2, 'b': 4}))
 
+    def test_partial_genericalias(self):
+        alias = self.partial[int]
+        self.assertIs(alias.__origin__, self.partial)
+        self.assertEqual(alias.__args__, (int,))
+        self.assertEqual(alias.__parameters__, ())
+
 
 @unittest.skipUnless(c_functools, 'requires the C _functools module')
 class TestPartialC(TestPartial, unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst b/Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst
new file mode 100644 (file)
index 0000000..5e39933
--- /dev/null
@@ -0,0 +1,5 @@
+Add missing ``__class_getitem__`` method to the Python implementation of
+:func:`functools.partial`, to make it compatible with the C version. This is
+mainly relevant for alternative Python implementations like PyPy and
+GraalPy, because CPython will usually use the C-implementation of that
+function.