From f7203a8fc4a8244a3a6bb3664293cc2d88361fa4 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Tue, 7 Apr 2020 16:55:53 -0700 Subject: [PATCH 1/3] Make functools.cached_property, partial, ... ... partialmethod, _lru_cache_wrapper generic --- Lib/functools.py | 6 ++++++ Lib/test/test_genericalias.py | 2 ++ Modules/_functoolsmodule.c | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/Lib/functools.py b/Lib/functools.py index 70fcec5a8f6d6c..f05b106b62c007 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -20,6 +20,7 @@ # import types, weakref # Deferred to single_dispatch() from reprlib import recursive_repr from _thread import RLock +from types import GenericAlias ################################################################################ @@ -656,6 +657,9 @@ def __get__(self, obj, cls=None): def __isabstractmethod__(self): return getattr(self.func, "__isabstractmethod__", False) + __class_getitem__ = classmethod(GenericAlias) + + # Helper functions def _unwrap_partial(func): @@ -1208,3 +1212,5 @@ def __get__(self, instance, owner=None): ) raise TypeError(msg) from None return val + + __class_getitem__ = classmethod(GenericAlias) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 9c5b23e5e5b0fc..147e8d9a6d5dfa 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -7,6 +7,7 @@ ) from collections.abc import * from contextlib import AbstractContextManager, AbstractAsyncContextManager +from functools import partial, partialmethod, _lru_cache_wrapper, cached_property from os import DirEntry from re import Pattern, Match from types import GenericAlias, MappingProxyType @@ -23,6 +24,7 @@ def test_subscriptable(self): defaultdict, deque, OrderedDict, Counter, UserDict, UserList, Pattern, Match, + partial, partialmethod, _lru_cache_wrapper, cached_property, AbstractContextManager, AbstractAsyncContextManager, Awaitable, Coroutine, AsyncIterable, AsyncIterator, diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index f3d8658044b997..9306b4fec150f2 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -414,6 +414,8 @@ partial_setstate(partialobject *pto, PyObject *state) static PyMethodDef partial_methods[] = { {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS}, {"__setstate__", (PyCFunction)partial_setstate, METH_O}, + {"__class_getitem__", (PyCFunction)Py_GenericAlias, + METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* sentinel */ }; @@ -1346,6 +1348,8 @@ static PyMethodDef lru_cache_methods[] = { {"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS}, {"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS}, {"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS}, + {"__class_getitem__", (PyCFunction)Py_GenericAlias, + METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL} }; From 8a35503c06a2d0e8a20ba7863c0dee00a754ca49 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Sun, 12 Apr 2020 23:29:08 -0700 Subject: [PATCH 2/3] Don't make _lru_cache_wrapper generic yet --- Modules/_functoolsmodule.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 9306b4fec150f2..2f1b47a5b061d0 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1348,8 +1348,6 @@ static PyMethodDef lru_cache_methods[] = { {"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS}, {"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS}, {"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS}, - {"__class_getitem__", (PyCFunction)Py_GenericAlias, - METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL} }; From b0907466058d1dfd8d004b974e731daa90e2c9c4 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Mon, 13 Apr 2020 05:12:49 -0700 Subject: [PATCH 3/3] Remove lru_cache test --- Lib/test/test_genericalias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 0b09a03a5d5900..0bedfc1e081dba 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -46,7 +46,7 @@ def test_subscriptable(self): FileInput, OrderedDict, Counter, UserDict, UserList, Pattern, Match, - partial, partialmethod, _lru_cache_wrapper, cached_property, + partial, partialmethod, cached_property, AbstractContextManager, AbstractAsyncContextManager, Awaitable, Coroutine, AsyncIterable, AsyncIterator,