bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)

This commit is contained in:
Ethan Smith 2020-04-14 16:14:15 -07:00 committed by GitHub
parent 33986465bd
commit d01628e411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 14 deletions

View File

@ -7,6 +7,7 @@ import keyword
import builtins import builtins
import functools import functools
import _thread import _thread
from types import GenericAlias
__all__ = ['dataclass', __all__ = ['dataclass',
@ -284,6 +285,8 @@ class Field:
# it. # it.
func(self.default, owner, name) func(self.default, owner, name)
__class_getitem__ = classmethod(GenericAlias)
class _DataclassParams: class _DataclassParams:
__slots__ = ('init', __slots__ = ('init',

View File

@ -18,6 +18,7 @@ import email.message
import email.generator import email.generator
import io import io
import contextlib import contextlib
from types import GenericAlias
try: try:
import fcntl import fcntl
except ImportError: except ImportError:
@ -260,6 +261,8 @@ class Mailbox:
else: else:
raise TypeError('Invalid message type: %s' % type(message)) raise TypeError('Invalid message type: %s' % type(message))
__class_getitem__ = classmethod(GenericAlias)
class Maildir(Mailbox): class Maildir(Mailbox):
"""A qmail-style Maildir mailbox.""" """A qmail-style Maildir mailbox."""
@ -2015,6 +2018,8 @@ class _ProxyFile:
return False return False
return self._file.closed return self._file.closed
__class_getitem__ = classmethod(GenericAlias)
class _PartialFile(_ProxyFile): class _PartialFile(_ProxyFile):
"""A read-only wrapper of part of a file.""" """A read-only wrapper of part of a file."""

View File

@ -358,10 +358,6 @@ class ContextTest(unittest.TestCase):
tp.shutdown() tp.shutdown()
self.assertEqual(results, list(range(10))) self.assertEqual(results, list(range(10)))
def test_contextvar_getitem(self):
clss = contextvars.ContextVar
self.assertEqual(clss[str], clss)
# HAMT Tests # HAMT Tests

View File

@ -9,7 +9,10 @@ from collections.abc import *
from concurrent.futures import Future from concurrent.futures import Future
from concurrent.futures.thread import _WorkItem from concurrent.futures.thread import _WorkItem
from contextlib import AbstractContextManager, AbstractAsyncContextManager 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 ctypes import Array, LibraryLoader
from difflib import SequenceMatcher from difflib import SequenceMatcher
from filecmp import dircmp from filecmp import dircmp
@ -60,6 +63,9 @@ class BaseTest(unittest.TestCase):
Reversible, Reversible,
Container, Collection, Container, Collection,
Callable, Callable,
Mailbox, _PartialFile,
ContextVar, Token,
Field,
Set, MutableSet, Set, MutableSet,
Mapping, MutableMapping, MappingView, Mapping, MutableMapping, MappingView,
KeysView, ItemsView, ValuesView, KeysView, ItemsView, ValuesView,

View File

@ -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[] = { static PyMemberDef PyContextVar_members[] = {
{"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY}, {"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY},
{NULL} {NULL}
@ -1040,8 +1033,8 @@ static PyMethodDef PyContextVar_methods[] = {
_CONTEXTVARS_CONTEXTVAR_GET_METHODDEF _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
_CONTEXTVARS_CONTEXTVAR_SET_METHODDEF _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF
_CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF
{"__class_getitem__", contextvar_cls_getitem, {"__class_getitem__", (PyCFunction)Py_GenericAlias,
METH_O | METH_CLASS, NULL}, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} {NULL, NULL}
}; };
@ -1180,10 +1173,17 @@ static PyGetSetDef PyContextTokenType_getsetlist[] = {
{NULL} {NULL}
}; };
static PyMethodDef PyContextTokenType_methods[] = {
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL}
};
PyTypeObject PyContextToken_Type = { PyTypeObject PyContextToken_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0) PyVarObject_HEAD_INIT(&PyType_Type, 0)
"Token", "Token",
sizeof(PyContextToken), sizeof(PyContextToken),
.tp_methods = PyContextTokenType_methods,
.tp_getset = PyContextTokenType_getsetlist, .tp_getset = PyContextTokenType_getsetlist,
.tp_dealloc = (destructor)token_tp_dealloc, .tp_dealloc = (destructor)token_tp_dealloc,
.tp_getattro = PyObject_GenericGetAttr, .tp_getattro = PyObject_GenericGetAttr,