]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39481: Make functools.cached_property, partial, partialmethod generic (#19427)
authorEthan Smith <ethan@ethanhs.me>
Tue, 14 Apr 2020 04:53:04 +0000 (21:53 -0700)
committerGitHub <noreply@github.com>
Tue, 14 Apr 2020 04:53:04 +0000 (21:53 -0700)
Lib/functools.py
Lib/test/test_genericalias.py
Modules/_functoolsmodule.c

index 70fcec5a8f6d6ccffa4e119b5a25efeaf0f52b80..f05b106b62c007fb47e4596256663df421724ef5 100644 (file)
@@ -20,6 +20,7 @@ from collections import namedtuple
 # import types, weakref  # Deferred to single_dispatch()
 from reprlib import recursive_repr
 from _thread import RLock
+from types import GenericAlias
 
 
 ################################################################################
@@ -656,6 +657,9 @@ class partialmethod(object):
     def __isabstractmethod__(self):
         return getattr(self.func, "__isabstractmethod__", False)
 
+    __class_getitem__ = classmethod(GenericAlias)
+
+
 # Helper functions
 
 def _unwrap_partial(func):
@@ -1208,3 +1212,5 @@ class cached_property:
                         )
                         raise TypeError(msg) from None
         return val
+
+    __class_getitem__ = classmethod(GenericAlias)
index 02b72838277e995016ca98306a12c261e871a47b..770aeef45105b5d53197c3018806f18d576355b0 100644 (file)
@@ -9,6 +9,7 @@ from collections.abc import *
 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 @@ class BaseTest(unittest.TestCase):
                   FileInput,
                   OrderedDict, Counter, UserDict, UserList,
                   Pattern, Match,
+                  partial, partialmethod, cached_property,
                   AbstractContextManager, AbstractAsyncContextManager,
                   Awaitable, Coroutine,
                   AsyncIterable, AsyncIterator,
index f3d8658044b997e4be1a5c0a9dd5d2199d3f6de3..2f1b47a5b061d053cf5e1da363a994fe637cb024 100644 (file)
@@ -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 */
 };