]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-127537: Add __class_getitem__ to the python implementation of functools...
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Fri, 27 Dec 2024 01:25:07 +0000 (17:25 -0800)
committerGitHub <noreply@github.com>
Fri, 27 Dec 2024 01:25:07 +0000 (01:25 +0000)
gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537)

(cherry picked from commit 401bba6b58497ce59e7b45ad33e43ae8c67abcb9)

Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
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 f6849899e7578e9a3fd1c1bbdcee2afeb22c021e..6025032bfaa233b7056686b3f9075a9e09a21241 100644 (file)
@@ -340,6 +340,9 @@ class partial:
         self.args = args
         self.keywords = kwds
 
+    __class_getitem__ = classmethod(GenericAlias)
+
+
 try:
     from _functools import partial
 except ImportError:
index ff71fd53c08a69069863c1a11fd223d2933d2556..dadcd04394b390347571af479df9e73e1059d280 100644 (file)
@@ -390,6 +390,13 @@ class TestPartial:
         f = self.partial(object)
         self.assertRaises(TypeError, f.__setstate__, BadSequence())
 
+    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):
     if c_functools:
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.