8000 [3.12] gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537) by hauntsaninja · Pull Request #128282 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.12] gh-127537: Add __class_getitem__ to the python implementation of functools.partial (#127537) #128282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ def __setstate__(self, state):
self.args = args
self.keywords = kwds

__class_getitem__ = classmethod(GenericAlias)


try:
from _functools import partial
except ImportError:
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ def __getitem__(self, key):
f = self.partial(object)
self.assertRaises(TypeError, f.__setstate__, BadSequence())

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):
if c_functools:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
< 449F span class='blob-code-inner blob-code-marker ' data-code-marker="+">GraalPy, because CPython will usually use the C-implementation of that
function.
Loading
0