[Python-checkins] bpo-39481: Make functools.cached_property, partial, partialmethod generic (#19427)

Ethan Smith webhook-mailer at python.org
Tue Apr 14 00:53:12 EDT 2020


https://github.com/python/cpython/commit/cecf049673da6a24435acd1a6a3b34472b323c97
commit: cecf049673da6a24435acd1a6a3b34472b323c97
branch: master
author: Ethan Smith <ethan at ethanhs.me>
committer: GitHub <noreply at github.com>
date: 2020-04-13T21:53:04-07:00
summary:

bpo-39481: Make functools.cached_property, partial, partialmethod generic (#19427)

files:
M Lib/functools.py
M Lib/test/test_genericalias.py
M Modules/_functoolsmodule.c

diff --git a/Lib/functools.py b/Lib/functools.py
index 70fcec5a8f6d6..f05b106b62c00 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 02b72838277e9..770aeef45105b 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -9,6 +9,7 @@
 from concurrent.futures import Future
 from concurrent.futures.thread import _WorkItem
 from contextlib import AbstractContextManager, AbstractAsyncContextManager
+from functools import partial, partialmethod, _lru_cache_wrapper, cached_property
 from ctypes import Array, LibraryLoader
 from difflib import SequenceMatcher
 from filecmp import dircmp
@@ -49,6 +50,7 @@ def test_subscriptable(self):
                   FileInput,
                   OrderedDict, Counter, UserDict, UserList,
                   Pattern, Match,
+                  partial, partialmethod, cached_property,
                   AbstractContextManager, AbstractAsyncContextManager,
                   Awaitable, Coroutine,
                   AsyncIterable, AsyncIterator,
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index f3d8658044b99..2f1b47a5b061d 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 */
 };
 



More information about the Python-checkins mailing list