Skip to content

Commit cecf049

Browse files
authored
bpo-39481: Make functools.cached_property, partial, partialmethod generic (#19427)
1 parent 584a3cf commit cecf049

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

Lib/functools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# import types, weakref # Deferred to single_dispatch()
2121
from reprlib import recursive_repr
2222
from _thread import RLock
23+
from types import GenericAlias
2324

2425

2526
################################################################################
@@ -656,6 +657,9 @@ def __get__(self, obj, cls=None):
656657
def __isabstractmethod__(self):
657658
return getattr(self.func, "__isabstractmethod__", False)
658659

660+
__class_getitem__ = classmethod(GenericAlias)
661+
662+
659663
# Helper functions
660664

661665
def _unwrap_partial(func):
@@ -1208,3 +1212,5 @@ def __get__(self, instance, owner=None):
12081212
)
12091213
raise TypeError(msg) from None
12101214
return val
1215+
1216+
__class_getitem__ = classmethod(GenericAlias)

Lib/test/test_genericalias.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from concurrent.futures import Future
1010
from concurrent.futures.thread import _WorkItem
1111
from contextlib import AbstractContextManager, AbstractAsyncContextManager
12+
from functools import partial, partialmethod, _lru_cache_wrapper, cached_property
1213
from ctypes import Array, LibraryLoader
1314
from difflib import SequenceMatcher
1415
from filecmp import dircmp
@@ -49,6 +50,7 @@ def test_subscriptable(self):
4950
FileInput,
5051
OrderedDict, Counter, UserDict, UserList,
5152
Pattern, Match,
53+
partial, partialmethod, cached_property,
5254
AbstractContextManager, AbstractAsyncContextManager,
5355
Awaitable, Coroutine,
5456
AsyncIterable, AsyncIterator,

Modules/_functoolsmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ partial_setstate(partialobject *pto, PyObject *state)
414414
static PyMethodDef partial_methods[] = {
415415
{"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
416416
{"__setstate__", (PyCFunction)partial_setstate, METH_O},
417+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
418+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
417419
{NULL, NULL} /* sentinel */
418420
};
419421

0 commit comments

Comments
 (0)