8000 [3.13] gh-127537: Add __class_getitem__ to the python implementation … · python/cpython@1781525 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1781525

Browse files
[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 401bba6) Co-authored-by: CF Bolz-Tereick <cfbolz@gmx.de>
1 parent 4f59f1d commit 1781525

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/functools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ def __setstate__(self, state):
351351
self.args = args
352352
self.keywords = kwds
353353

354+
__class_getitem__ = classmethod(GenericAlias)
355+
356+
354357
try:
355358
from _functools import partial
356359
except ImportError:

Lib/test/test_functools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ class A:
411411
self.assertEqual(a.cmeth(3, b=4), ((1, A, 3), {'a': 2, 'b': 4}))
412412
self.assertEqual(a.smeth(3, b=4), ((1, 3), {'a': 2, 'b': 4}))
413413

414+
def test_partial_genericalias(self):
415+
alias = self.partial[int]
416+
self.assertIs(alias.__origin__, self.partial)
417+
self.assertEqual(alias.__args__, (int,))
418+
self.assertEqual(alias.__parameters__, ())
419+
414420

415421
@unittest.skipUnless(c_functools, 'requires the C _functools module')
416422
class TestPartialC(TestPartial, unittest.TestCase):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Add missing ``__class_getitem__`` method to the Python implementation of
2+
:func:`functools.partial`, to make it compatible with the C version. This is
3+
mainly relevant for alternative Python implementations like PyPy and
4+
GraalPy, because CPython will usually use the C-implementation of that
5+
function.

0 commit comments

Comments
 (0)
0