From 7c110c8565f213e18f8d02adf61ddccd94892c39 Mon Sep 17 00:00:00 2001 From: CF Bolz-Tereick Date: Mon, 2 Dec 2024 20:47:06 +0100 Subject: [PATCH 1/3] add __class_getitem__ to the python implementation of functools.partial --- Lib/functools.py | 3 +++ Lib/test/test_functools.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Lib/functools.py b/Lib/functools.py index eff6540c7f606e..786b8aedfd77f5 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -433,6 +433,9 @@ def __setstate__(self, state): self._phcount = phcount self._merger = merger + __class_getitem__ = classmethod(GenericAlias) + + try: from _functools import partial, Placeholder, _PlaceholderType except ImportError: diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 6d60f6941c4c5d..9e6ed0fd0a500f 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -472,6 +472,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): From f519e33e34ad7a86155312bd7f4db36d1b94e48f Mon Sep 17 00:00:00 2001 From: CF Bolz-Tereick Date: Wed, 4 Dec 2024 10:39:31 +0100 Subject: [PATCH 2/3] add news entry --- .../Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst 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..84cb3067a6bd7a --- /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. From 1dc2c1c5cecdb357ae014830d369259a1307aae7 Mon Sep 17 00:00:00 2001 From: CF Bolz-Tereick Date: Wed, 4 Dec 2024 10:43:41 +0100 Subject: [PATCH 3/3] double backticks --- .../next/Library/2024-12-04-10-39-29.gh-issue-83662.CG1s3m.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 84cb3067a6bd7a..5e39933047993c 100644 --- 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 @@ -1,4 +1,4 @@ -Add missing `__class_getitem__` method to the Python implementation of +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