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

Skip to content

Commit 401bba6

Browse files
authored
gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537)
1 parent ea2b537 commit 401bba6

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
@@ -433,6 +433,9 @@ def __setstate__(self, state):
433433
self._phcount = phcount
434434
self._merger = merger
435435

436+
__class_getitem__ = classmethod(GenericAlias)
437+
438+
436439
try:
437440
from _functools import partial, Placeholder, _PlaceholderType
438441
except ImportError:

Lib/test/test_functools.py

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

476+
def test_partial_genericalias(self):
477+
alias = self.partial[int]
478+
self.assertIs(alias.__origin__, self.partial)
479+
self.assertEqual(alias.__args__, (int,))
480+
self.assertEqual(alias.__parameters__, ())
481+
476482

477483
@unittest.skipUnless(c_functools, 'requires the C _functools module')
478484
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