Skip to content

Commit d01628e

Browse files
authored
bpo-39481: PEP 585 for dataclasses, mailbox, contextvars (GH-19425)
1 parent 3398646 commit d01628e

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

Lib/dataclasses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import builtins
88
import functools
99
import _thread
10+
from types import GenericAlias
1011

1112

1213
__all__ = ['dataclass',
@@ -284,6 +285,8 @@ def __set_name__(self, owner, name):
284285
# it.
285286
func(self.default, owner, name)
286287

288+
__class_getitem__ = classmethod(GenericAlias)
289+
287290

288291
class _DataclassParams:
289292
__slots__ = ('init',

Lib/mailbox.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import email.generator
1919
import io
2020
import contextlib
21+
from types import GenericAlias
2122
try:
2223
import fcntl
2324
except ImportError:
@@ -260,6 +261,8 @@ def _dump_message(self, message, target, mangle_from_=False):
260261
else:
261262
raise TypeError('Invalid message type: %s' % type(message))
262263

264+
__class_getitem__ = classmethod(GenericAlias)
265+
263266

264267
class Maildir(Mailbox):
265268
"""A qmail-style Maildir mailbox."""
@@ -2015,6 +2018,8 @@ def closed(self):
20152018
return False
20162019
return self._file.closed
20172020

2021+
__class_getitem__ = classmethod(GenericAlias)
2022+
20182023

20192024
class _PartialFile(_ProxyFile):
20202025
"""A read-only wrapper of part of a file."""

Lib/test/test_context.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ def sub(num):
358358
tp.shutdown()
359359
self.assertEqual(results, list(range(10)))
360360

361-
def test_contextvar_getitem(self):
362-
clss = contextvars.ContextVar
363-
self.assertEqual(clss[str], clss)
364-
365361

366362
# HAMT Tests
367363

Lib/test/test_genericalias.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
from concurrent.futures import Future
1010
from concurrent.futures.thread import _WorkItem
1111
from contextlib import AbstractContextManager, AbstractAsyncContextManager
12-
from functools import partial, partialmethod, _lru_cache_wrapper, cached_property
12+
from contextvars import ContextVar, Token
13+
from dataclasses import Field
14+
from functools import partial, partialmethod, cached_property
15+
from mailbox import Mailbox, _PartialFile
1316
from ctypes import Array, LibraryLoader
1417
from difflib import SequenceMatcher
1518
from filecmp import dircmp
@@ -60,6 +63,9 @@ def test_subscriptable(self):
6063
Reversible,
6164
Container, Collection,
6265
Callable,
66+
Mailbox, _PartialFile,
67+
ContextVar, Token,
68+
Field,
6369
Set, MutableSet,
6470
Mapping, MutableMapping, MappingView,
6571
KeysView, ItemsView, ValuesView,

Python/context.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,13 +1024,6 @@ _contextvars_ContextVar_reset(PyContextVar *self, PyObject *token)
10241024
}
10251025

10261026

1027-
static PyObject *
1028-
contextvar_cls_getitem(PyObject *self, PyObject *arg)
1029-
{
1030-
Py_INCREF(self);
1031-
return self;
1032-
}
1033-
10341027
static PyMemberDef PyContextVar_members[] = {
10351028
{"name", T_OBJECT, offsetof(PyContextVar, var_name), READONLY},
10361029
{NULL}
@@ -1040,8 +1033,8 @@ static PyMethodDef PyContextVar_methods[] = {
10401033
_CONTEXTVARS_CONTEXTVAR_GET_METHODDEF
10411034
_CONTEXTVARS_CONTEXTVAR_SET_METHODDEF
10421035
_CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF
1043-
{"__class_getitem__", contextvar_cls_getitem,
1044-
METH_O | METH_CLASS, NULL},
1036+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
1037+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
10451038
{NULL, NULL}
10461039
};
10471040

@@ -1180,10 +1173,17 @@ static PyGetSetDef PyContextTokenType_getsetlist[] = {
11801173
{NULL}
11811174
};
11821175

1176+
static PyMethodDef PyContextTokenType_methods[] = {
1177+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
1178+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
1179+
{NULL}
1180+
};
1181+
11831182
PyTypeObject PyContextToken_Type = {
11841183
PyVarObject_HEAD_INIT(&PyType_Type, 0)
11851184
"Token",
11861185
sizeof(PyContextToken),
1186+
.tp_methods = PyContextTokenType_methods,
11871187
.tp_getset = PyContextTokenType_getsetlist,
11881188
.tp_dealloc = (destructor)token_tp_dealloc,
11891189
.tp_getattro = PyObject_GenericGetAttr,

0 commit comments

Comments
 (0)