Skip to content

Commit 0361556

Browse files
authored
bpo-39481: PEP 585 for a variety of modules (GH-19423)
- concurrent.futures - ctypes - http.cookies - multiprocessing - queue - tempfile - unittest.case - urllib.parse
1 parent e3ec44d commit 0361556

File tree

15 files changed

+66
-1
lines changed

15 files changed

+66
-1
lines changed

Lib/concurrent/futures/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import threading
99
import time
10+
import types
1011

1112
FIRST_COMPLETED = 'FIRST_COMPLETED'
1213
FIRST_EXCEPTION = 'FIRST_EXCEPTION'
@@ -544,6 +545,8 @@ def set_exception(self, exception):
544545
self._condition.notify_all()
545546
self._invoke_callbacks()
546547

548+
__class_getitem__ = classmethod(types.GenericAlias)
549+
547550
class Executor(object):
548551
"""This is an abstract base class for concrete asynchronous executors."""
549552

Lib/concurrent/futures/thread.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import itertools
1111
import queue
1212
import threading
13+
import types
1314
import weakref
1415
import os
1516

@@ -57,6 +58,8 @@ def run(self):
5758
else:
5859
self.future.set_result(result)
5960

61+
__class_getitem__ = classmethod(types.GenericAlias)
62+
6063

6164
def _worker(executor_reference, work_queue, initializer, initargs):
6265
if initializer is not None:

Lib/ctypes/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""create and manipulate C data types in Python"""
22

33
import os as _os, sys as _sys
4+
import types as _types
45

56
__version__ = "1.1.0"
67

@@ -450,6 +451,8 @@ def __getitem__(self, name):
450451
def LoadLibrary(self, name):
451452
return self._dlltype(name)
452453

454+
__class_getitem__ = classmethod(_types.GenericAlias)
455+
453456
cdll = LibraryLoader(CDLL)
454457
pydll = LibraryLoader(PyDLL)
455458

Lib/http/cookies.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#
132132
import re
133133
import string
134+
import types
134135

135136
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
136137

@@ -419,6 +420,8 @@ def OutputString(self, attrs=None):
419420
# Return the result
420421
return _semispacejoin(result)
421422

423+
__class_getitem__ = classmethod(types.GenericAlias)
424+
422425

423426
#
424427
# Pattern for finding cookie

Lib/multiprocessing/managers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import array
2222
import queue
2323
import time
24+
import types
2425
import os
2526
from os import getpid
2627

@@ -1129,6 +1130,8 @@ def set(self, value):
11291130
return self._callmethod('set', (value,))
11301131
value = property(get, set)
11311132

1133+
__class_getitem__ = classmethod(types.GenericAlias)
1134+
11321135

11331136
BaseListProxy = MakeProxyType('BaseListProxy', (
11341137
'__add__', '__contains__', '__delitem__', '__getitem__', '__len__',

Lib/multiprocessing/pool.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import threading
2121
import time
2222
import traceback
23+
import types
2324
import warnings
2425
from queue import Empty
2526

@@ -780,6 +781,8 @@ def _set(self, i, obj):
780781
del self._cache[self._job]
781782
self._pool = None
782783

784+
__class_getitem__ = classmethod(types.GenericAlias)
785+
783786
AsyncResult = ApplyResult # create alias -- see #17805
784787

785788
#

Lib/multiprocessing/queues.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import threading
1515
import collections
1616
import time
17+
import types
1718
import weakref
1819
import errno
1920

@@ -366,3 +367,5 @@ def put(self, obj):
366367
else:
367368
with self._wlock:
368369
self._writer.send_bytes(obj)
370+
371+
__class_getitem__ = classmethod(types.GenericAlias)

Lib/multiprocessing/shared_memory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import errno
1515
import struct
1616
import secrets
17+
import types
1718

1819
if os.name == "nt":
1920
import _winapi
@@ -508,3 +509,5 @@ def index(self, value):
508509
return position
509510
else:
510511
raise ValueError(f"{value!r} not in this container")
512+
513+
__class_getitem__ = classmethod(types.GenericAlias)

Lib/queue.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'''A multi-producer, multi-consumer queue.'''
22

33
import threading
4+
import types
45
from collections import deque
56
from heapq import heappush, heappop
67
from time import monotonic as time
@@ -216,6 +217,8 @@ def _put(self, item):
216217
def _get(self):
217218
return self.queue.popleft()
218219

220+
__class_getitem__ = classmethod(types.GenericAlias)
221+
219222

220223
class PriorityQueue(Queue):
221224
'''Variant of Queue that retrieves open entries in priority order (lowest first).
@@ -316,6 +319,8 @@ def qsize(self):
316319
'''Return the approximate size of the queue (not reliable!).'''
317320
return len(self._queue)
318321

322+
__class_getitem__ = classmethod(types.GenericAlias)
323+
319324

320325
if SimpleQueue is None:
321326
SimpleQueue = _PySimpleQueue

Lib/tempfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,5 @@ def __exit__(self, exc, value, tb):
829829
def cleanup(self):
830830
if self._finalizer.detach():
831831
self._rmtree(self.name)
832+
833+
__class_getitem__ = classmethod(_types.GenericAlias)

Lib/test/test_genericalias.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@
66
defaultdict, deque, OrderedDict, Counter, UserDict, UserList
77
)
88
from collections.abc import *
9+
from concurrent.futures import Future
10+
from concurrent.futures.thread import _WorkItem
911
from contextlib import AbstractContextManager, AbstractAsyncContextManager
12+
from ctypes import Array, LibraryLoader
1013
from difflib import SequenceMatcher
1114
from filecmp import dircmp
1215
from fileinput import FileInput
1316
from mmap import mmap
1417
from ipaddress import IPv4Network, IPv4Interface, IPv6Network, IPv6Interface
1518
from itertools import chain
19+
from http.cookies import Morsel
20+
from multiprocessing.managers import ValueProxy
21+
from multiprocessing.pool import ApplyResult
22+
from multiprocessing.shared_memory import ShareableList
23+
from multiprocessing.queues import SimpleQueue
1624
from os import DirEntry
1725
from re import Pattern, Match
1826
from types import GenericAlias, MappingProxyType, AsyncGeneratorType
27+
from tempfile import TemporaryDirectory, SpooledTemporaryFile
28+
from urllib.parse import SplitResult, ParseResult
29+
from unittest.case import _AssertRaisesContext
30+
from queue import Queue, SimpleQueue
1931
import typing
2032

2133
from typing import TypeVar
@@ -49,6 +61,15 @@ def test_subscriptable(self):
4961
DirEntry,
5062
IPv4Network, IPv4Interface, IPv6Network, IPv6Interface,
5163
chain,
64+
TemporaryDirectory, SpooledTemporaryFile,
65+
Queue, SimpleQueue,
66+
_AssertRaisesContext,
67+
Array, LibraryLoader,
68+
SplitResult, ParseResult,
69+
ValueProxy, ApplyResult,
70+
ShareableList, SimpleQueue,
71+
Future, _WorkItem,
72+
Morsel,
5273
):
5374
tname = t.__name__
5475
with self.subTest(f"Testing {tname}"):

Lib/unittest/case.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ def __exit__(self, exc_type, exc_value, tb):
241241
expected_regex.pattern, str(exc_value)))
242242
return True
243243

244+
__class_getitem__ = classmethod(types.GenericAlias)
245+
244246

245247
class _AssertWarnsContext(_AssertRaisesBaseContext):
246248
"""A context manager used to implement TestCase.assertWarns* methods."""

Lib/urllib/parse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import re
3131
import sys
32+
import types
3233
import collections
3334
import warnings
3435

@@ -176,6 +177,8 @@ def port(self):
176177
raise ValueError("Port out of range 0-65535")
177178
return port
178179

180+
__class_getitem__ = classmethod(types.GenericAlias)
181+
179182

180183
class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr):
181184
__slots__ = ()

Modules/_ctypes/_ctypes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4798,6 +4798,12 @@ Array_length(PyObject *myself)
47984798
return self->b_length;
47994799
}
48004800

4801+
static PyMethodDef Array_methods[] = {
4802+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
4803+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
4804+
{ NULL, NULL }
4805+
};
4806+
48014807
static PySequenceMethods Array_as_sequence = {
48024808
Array_length, /* sq_length; */
48034809
0, /* sq_concat; */
@@ -4846,7 +4852,7 @@ PyTypeObject PyCArray_Type = {
48464852
0, /* tp_weaklistoffset */
48474853
0, /* tp_iter */
48484854
0, /* tp_iternext */
4849-
0, /* tp_methods */
4855+
Array_methods, /* tp_methods */
48504856
0, /* tp_members */
48514857
0, /* tp_getset */
48524858
0, /* tp_base */

Modules/_queuemodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ static PyMethodDef simplequeue_methods[] = {
302302
_QUEUE_SIMPLEQUEUE_PUT_METHODDEF
303303
_QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF
304304
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
305+
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
306+
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
305307
{NULL, NULL} /* sentinel */
306308
};
307309

0 commit comments

Comments
 (0)