Skip to content

Commit 177a13e

Browse files
committed
Make dataclasses.Field, contextvars.Token, ...
... mailbox.MailBox, and mailbox._ProxyFile generic.
1 parent 9205520 commit 177a13e

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
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_genericalias.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
)
88
from collections.abc import *
99
from contextlib import AbstractContextManager, AbstractAsyncContextManager
10+
from contextvars import Token
11+
from dataclasses import Field
12+
from mailbox import Mailbox, _PartialFile
1013
from os import DirEntry
1114
from re import Pattern, Match
1215
from types import GenericAlias, MappingProxyType
@@ -31,6 +34,9 @@ def test_subscriptable(self):
3134
Reversible,
3235
Container, Collection,
3336
Callable,
37+
Mailbox, _PartialFile,
38+
Token,
39+
Field,
3440
Set, MutableSet,
3541
Mapping, MutableMapping, MappingView,
3642
KeysView, ItemsView, ValuesView,

Python/context.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,17 @@ static PyGetSetDef PyContextTokenType_getsetlist[] = {
11791179
{NULL}
11801180
};
11811181

1182+
static PyMethodDef PyContextTokenType_methods[] = {
1183+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
1184+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
1185+
{NULL}
1186+
};
1187+
11821188
PyTypeObject PyContextToken_Type = {
11831189
PyVarObject_HEAD_INIT(&PyType_Type, 0)
11841190
"Token",
11851191
sizeof(PyContextToken),
1192+
.tp_methods = PyContextTokenType_methods,
11861193
.tp_getset = PyContextTokenType_getsetlist,
11871194
.tp_dealloc = (destructor)token_tp_dealloc,
11881195
.tp_getattro = PyObject_GenericGetAttr,

0 commit comments

Comments
 (0)