Skip to content

bpo-39481: PEP 585 for a variety of modules #19423

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 3 commits into from
Apr 10, 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/concurrent/futures/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import threading
import time
import types

FIRST_COMPLETED = 'FIRST_COMPLETED'
FIRST_EXCEPTION = 'FIRST_EXCEPTION'
Expand Down Expand Up @@ -544,6 +545,8 @@ def set_exception(self, exception):
self._condition.notify_all()
self._invoke_callbacks()

__class_getitem__ = classmethod(types.GenericAlias)

class Executor(object):
"""This is an abstract base class for concrete asynchronous executors."""

Expand Down
3 changes: 3 additions & 0 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import itertools
import queue
import threading
import types
import weakref
import os

Expand Down Expand Up @@ -57,6 +58,8 @@ def run(self):
else:
self.future.set_result(result)

__class_getitem__ = classmethod(types.GenericAlias)


def _worker(executor_reference, work_queue, initializer, initargs):
if initializer is not None:
Expand Down
3 changes: 3 additions & 0 deletions Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""create and manipulate C data types in Python"""

import os as _os, sys as _sys
import types as _types

__version__ = "1.1.0"

Expand Down Expand Up @@ -450,6 +451,8 @@ def __getitem__(self, name):
def LoadLibrary(self, name):
return self._dlltype(name)

__class_getitem__ = classmethod(_types.GenericAlias)

cdll = LibraryLoader(CDLL)
pydll = LibraryLoader(PyDLL)

Expand Down
3 changes: 3 additions & 0 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
#
import re
import string
import types

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

Expand Down Expand Up @@ -419,6 +420,8 @@ def OutputString(self, attrs=None):
# Return the result
return _semispacejoin(result)

__class_getitem__ = classmethod(types.GenericAlias)


#
# Pattern for finding cookie
Expand Down
3 changes: 3 additions & 0 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import array
import queue
import time
import types
import os
from os import getpid

Expand Down Expand Up @@ -1129,6 +1130,8 @@ def set(self, value):
return self._callmethod('set', (value,))
value = property(get, set)

__class_getitem__ = classmethod(types.GenericAlias)


BaseListProxy = MakeProxyType('BaseListProxy', (
'__add__', '__contains__', '__delitem__', '__getitem__', '__len__',
Expand Down
3 changes: 3 additions & 0 deletions Lib/multiprocessing/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import threading
import time
import traceback
import types
import warnings
from queue import Empty

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

__class_getitem__ = classmethod(types.GenericAlias)

AsyncResult = ApplyResult # create alias -- see #17805

#
Expand Down
3 changes: 3 additions & 0 deletions Lib/multiprocessing/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import threading
import collections
import time
import types
import weakref
import errno

Expand Down Expand Up @@ -366,3 +367,5 @@ def put(self, obj):
else:
with self._wlock:
self._writer.send_bytes(obj)

__class_getitem__ = classmethod(types.GenericAlias)
3 changes: 3 additions & 0 deletions Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import errno
import struct
import secrets
import types

if os.name == "nt":
import _winapi
Expand Down Expand Up @@ -508,3 +509,5 @@ def index(self, value):
return position
else:
raise ValueError(f"{value!r} not in this container")

__class_getitem__ = classmethod(types.GenericAlias)
5 changes: 5 additions & 0 deletions Lib/queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''A multi-producer, multi-consumer queue.'''

import threading
import types
from collections import deque
from heapq import heappush, heappop
from time import monotonic as time
Expand Down Expand Up @@ -216,6 +217,8 @@ def _put(self, item):
def _get(self):
return self.queue.popleft()

__class_getitem__ = classmethod(types.GenericAlias)


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

__class_getitem__ = classmethod(types.GenericAlias)


if SimpleQueue is None:
SimpleQueue = _PySimpleQueue
2 changes: 2 additions & 0 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,5 @@ def __exit__(self, exc, value, tb):
def cleanup(self):
if self._finalizer.detach():
self._rmtree(self.name)

__class_getitem__ = classmethod(_types.GenericAlias)
21 changes: 21 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
defaultdict, deque, OrderedDict, Counter, UserDict, UserList
)
from collections.abc import *
from concurrent.futures import Future
from concurrent.futures.thread import _WorkItem
from contextlib import AbstractContextManager, AbstractAsyncContextManager
from ctypes import Array, LibraryLoader
from difflib import SequenceMatcher
from filecmp import dircmp
from fileinput import FileInput
from mmap import mmap
from ipaddress import IPv4Network, IPv4Interface, IPv6Network, IPv6Interface
from itertools import chain
from http.cookies import Morsel
from multiprocessing.managers import ValueProxy
from multiprocessing.pool import ApplyResult
from multiprocessing.shared_memory import ShareableList
from multiprocessing.queues import SimpleQueue
from os import DirEntry
from re import Pattern, Match
from types import GenericAlias, MappingProxyType, AsyncGeneratorType
from tempfile import TemporaryDirectory, SpooledTemporaryFile
from urllib.parse import SplitResult, ParseResult
from unittest.case import _AssertRaisesContext
from queue import Queue, SimpleQueue
import typing

from typing import TypeVar
Expand Down Expand Up @@ -49,6 +61,15 @@ def test_subscriptable(self):
DirEntry,
IPv4Network, IPv4Interface, IPv6Network, IPv6Interface,
chain,
TemporaryDirectory, SpooledTemporaryFile,
Queue, SimpleQueue,
_AssertRaisesContext,
Array, LibraryLoader,
SplitResult, ParseResult,
ValueProxy, ApplyResult,
ShareableList, SimpleQueue,
Future, _WorkItem,
Morsel,
):
tname = t.__name__
with self.subTest(f"Testing {tname}"):
Expand Down
2 changes: 2 additions & 0 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ def __exit__(self, exc_type, exc_value, tb):
expected_regex.pattern, str(exc_value)))
return True

__class_getitem__ = classmethod(types.GenericAlias)


class _AssertWarnsContext(_AssertRaisesBaseContext):
"""A context manager used to implement TestCase.assertWarns* methods."""
Expand Down
3 changes: 3 additions & 0 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import re
import sys
import types
import collections
import warnings

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

__class_getitem__ = classmethod(types.GenericAlias)


class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr):
__slots__ = ()
Expand Down
8 changes: 7 additions & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4798,6 +4798,12 @@ Array_length(PyObject *myself)
return self->b_length;
}

static PyMethodDef Array_methods[] = {
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{ NULL, NULL }
};

static PySequenceMethods Array_as_sequence = {
Array_length, /* sq_length; */
0, /* sq_concat; */
Expand Down Expand Up @@ -4846,7 +4852,7 @@ PyTypeObject PyCArray_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
Array_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
Expand Down
2 changes: 2 additions & 0 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ static PyMethodDef simplequeue_methods[] = {
_QUEUE_SIMPLEQUEUE_PUT_METHODDEF
_QUEUE_SIMPLEQUEUE_PUT_NOWAIT_METHODDEF
_QUEUE_SIMPLEQUEUE_QSIZE_METHODDEF
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* sentinel */
};

Expand Down