Skip to content

Commit 82dbfbf

Browse files
hauntsaninjacfbolz
andauthored
[3.12] gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537) (#128282)
gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537) (cherry picked from commit 401bba6) Co-authored-by: CF Bolz-Tereick <[email protected]>
1 parent f699151 commit 82dbfbf

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/functools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ def __setstate__(self, state):
340340
self.args = args
341341
self.keywords = kwds
342342

343+
__class_getitem__ = classmethod(GenericAlias)
344+
345+
343346
try:
344347
from _functools import partial
345348
except ImportError:

Lib/test/test_functools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ def __getitem__(self, key):
390390
f = self.partial(object)
391391
self.assertRaises(TypeError, f.__setstate__, BadSequence())
392392

393+
def test_partial_genericalias(self):
394+
alias = self.partial[int]
395+
self.assertIs(alias.__origin__, self.partial)
396+
self.assertEqual(alias.__args__, (int,))
397+
self.assertEqual(alias.__parameters__, ())
398+
399+
393400
@unittest.skipUnless(c_functools, 'requires the C _functools module')
394401
class TestPartialC(TestPartial, unittest.TestCase):
395402
if c_functools:
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)