diff options
author | Ethan Smith <ethan@ethanhs.me> | 2020-04-14 23:14:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-14 23:14:15 (GMT) |
commit | d01628e411752ee6849f862cae66a1c69fe512b7 (patch) | |
tree | 4b6fc066cc0dc4c1aa2352896d81c0573e5a9138 | |
parent | 33986465bde2a2188537c4ef6cdb6055e348f31f (diff) | |
download | cpython-d01628e411752ee6849f862cae66a1c69fe512b7.zip cpython-d01628e411752ee6849f862cae66a1c69fe512b7.tar.gz cpython-d01628e411752ee6849f862cae66a1c69fe512b7.tar.bz2 |
bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)
-rw-r--r-- | Lib/dataclasses.py | 3 | ||||
-rw-r--r-- | Lib/mailbox.py | 5 | ||||
-rw-r--r-- | Lib/test/test_context.py | 4 | ||||
-rw-r--r-- | Lib/test/test_genericalias.py | 8 | ||||
-rw-r--r-- | Python/context.c | 18 |
5 files changed, 24 insertions, 14 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 00851c6..fc69508 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -7,6 +7,7 @@ import keyword import builtins import functools import _thread +from types import GenericAlias __all__ = ['dataclass', @@ -284,6 +285,8 @@ class Field: # it. func(self.default, owner, name) + __class_getitem__ = classmethod(GenericAlias) + class _DataclassParams: __slots__ = ('init', diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 5b4e864..70da07e 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -18,6 +18,7 @@ import email.message import email.generator import io import contextlib +from types import GenericAlias try: import fcntl except ImportError: @@ -260,6 +261,8 @@ class Mailbox: else: raise TypeError('Invalid message type: %s' % type(message)) + __class_getitem__ = classmethod(GenericAlias) + class Maildir(Mailbox): """A qmail-style Maildir mailbox.""" @@ -2015,6 +2018,8 @@ class _ProxyFile: return False return self._file.closed + __class_getitem__ = classmethod(GenericAlias) + class _PartialFile(_ProxyFile): """A read-only wrapper of part of a file.""" diff --git a/Lib/test/test_context.py b/Lib/test/test_context.py index b9e991a..2d8b63a 100644 --- a/Lib/test/test_context.py +++ b/Lib/test/test_context.py @@ -358,10 +358,6 @@ class ContextTest(unittest.TestCase): tp.shutdown() self.assertEqual(results, list(range(10))) - def test_contextvar_getitem(self): - clss = contextvars.ContextVar - self.assertEqual(clss[str], clss) - # HAMT Tests diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 686df17..37cbf92 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -9,7 +9,10 @@ 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 contextvars import ContextVar, Token +from dataclasses import Field +from functools import partial, partialmethod, cached_property +from mailbox import Mailbox, _PartialFile from ctypes import Array, LibraryLoader from difflib import SequenceMatcher from filecmp import dircmp @@ -60,6 +63,9 @@ class BaseTest(unittest.TestCase): Reversible, Container, Collection, Callable, + Mailbox, _PartialFile, + ContextVar, Token, + Field, Set, MutableSet, Mapping, MutableMapping, MappingView, KeysView, ItemsView, ValuesView, diff --git a/Python/context.c b/Python/context.c index 00f25dd..15749e9 100644 --- a/Python/context.c +++ b/Python/context.c @@ -1024,13 +1024,6 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token) } -static PyObject * -contextvar_cls_getitem(PyObject *self, PyObject *arg) -{ - Py_INCREF(self); - return self; -} - static PyMemberDef PyContextVar_members[] = { {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY}, {NULL} @@ -1040,8 +1033,8 @@ static PyMethodDef PyContextVar_methods[] = { _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF - {"__class_getitem__", contextvar_cls_getitem, - METH_O | METH_CLASS, NULL}, + {"__class_getitem__", (PyCFunction)Py_GenericAlias, + METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} }; @@ -1180,10 +1173,17 @@ static PyGetSetDef PyContextTokenType_getsetlist[] = { {NULL} }; +static PyMethodDef PyContextTokenType_methods[] = { + {"__class_getitem__", (PyCFunction)Py_GenericAlias, + METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, + {NULL} +}; + PyTypeObject PyContextToken_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "Token", sizeof(PyContextToken), + .tp_methods = PyContextTokenType_methods, .tp_getset = PyContextTokenType_getsetlist, .tp_dealloc = (destructor)token_tp_dealloc, .tp_getattro = PyObject_GenericGetAttr, |