https://github.com/python/cpython/commit/1781525422c7162a0aec2fda1dc95e45123b419a
commit: 1781525422c7162a0aec2fda1dc95e45123b419a
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: hauntsaninja <[email protected]>
date: 2024-12-27T01:25:56Z
summary:

[3.13] gh-127537: Add __class_getitem__ to the python implementation of 
functools.partial (GH-127537) (#128281)

gh-127537: Add __class_getitem__ to the python implementation of 
functools.partial (GH-127537)
(cherry picked from commit 401bba6b58497ce59e7b45ad33e43ae8c67abcb9)

Co-authored-by: CF Bolz-Tereick <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst
M Lib/functools.py
M Lib/test/test_functools.py

diff --git a/Lib/functools.py b/Lib/functools.py
index e0140e84842a7d..2bc5053bd1b53f 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -351,6 +351,9 @@ def __setstate__(self, state):
         self.args = args
         self.keywords = kwds
 
+    __class_getitem__ = classmethod(GenericAlias)
+
+
 try:
     from _functools import partial
 except ImportError:
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 1ce0f4d0aea6ee..894adb699c87f2 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -411,6 +411,12 @@ class A:
         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
index 00000000000000..5e39933047993c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to