[Python-checkins] bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)

Ethan Smith webhook-mailer at python.org
Tue Apr 14 19:14:22 EDT 2020


https://github.com/python/cpython/commit/d01628e411752ee6849f862cae66a1c69fe512b7
commit: d01628e411752ee6849f862cae66a1c69fe512b7
branch: master
author: Ethan Smith <ethan at ethanhs.me>
committer: GitHub <noreply at github.com>
date: 2020年04月14日T16:14:15-07:00
summary:
bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)
files:
M Lib/dataclasses.py
M Lib/mailbox.py
M Lib/test/test_context.py
M Lib/test/test_genericalias.py
M Python/context.c
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 00851c648a13b..fc69508354bbe 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -7,6 +7,7 @@
 import builtins
 import functools
 import _thread
+from types import GenericAlias
 
 
 __all__ = ['dataclass',
@@ -284,6 +285,8 @@ def __set_name__(self, owner, name):
 # 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 5b4e86419f117..70da07ed2e9e8 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -18,6 +18,7 @@
 import email.generator
 import io
 import contextlib
+from types import GenericAlias
 try:
 import fcntl
 except ImportError:
@@ -260,6 +261,8 @@ def _dump_message(self, message, target, mangle_from_=False):
 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 @@ def closed(self):
 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 b9e991a400092..2d8b63a1f5958 100644
--- a/Lib/test/test_context.py
+++ b/Lib/test/test_context.py
@@ -358,10 +358,6 @@ def sub(num):
 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 686df17d6a961..37cbf92ed1161 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -9,7 +9,10 @@
 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 @@ def test_subscriptable(self):
 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 00f25ddab41d1..15749e9fd7741 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,


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /