Skip to content

bpo-39481: Make dataclasses.Field, contextvars.Token, ... #19425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import builtins
import functools
import _thread
from types import GenericAlias


__all__ = ['dataclass',
Expand Down Expand Up @@ -284,6 +285,8 @@ def __set_name__(self, owner, name):
# it.
func(self.default, owner, name)

__class_getitem__ = classmethod(GenericAlias)


class _DataclassParams:
__slots__ = ('init',
Expand Down
5 changes: 5 additions & 0 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import email.generator
import io
import contextlib
from types import GenericAlias
try:
import fcntl
except ImportError:
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions Python/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,13 +1023,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}
Expand All @@ -1039,8 +1032,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}
};

Expand Down Expand Up @@ -1179,10 +1172,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,
Expand Down