Skip to content

Commit 6ed874f

Browse files
[3.9] bpo-46469: Make asyncio generic classes return GenericAlias (GH-30777) (GH-30785)
Automerge-Triggered-By: GH:asvetlov
1 parent 3c4a374 commit 6ed874f

File tree

8 files changed

+29
-24
lines changed

8 files changed

+29
-24
lines changed

Lib/asyncio/futures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import contextvars
99
import logging
1010
import sys
11+
from types import GenericAlias
1112

1213
from . import base_futures
1314
from . import events
@@ -106,8 +107,7 @@ def __del__(self):
106107
context['source_traceback'] = self._source_traceback
107108
self._loop.call_exception_handler(context)
108109

109-
def __class_getitem__(cls, type):
110-
return cls
110+
__class_getitem__ = classmethod(GenericAlias)
111111

112112
@property
113113
def _log_traceback(self):

Lib/asyncio/queues.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import heapq
55
import warnings
6+
from types import GenericAlias
67

78
from . import events
89
from . import locks
@@ -76,8 +77,7 @@ def __repr__(self):
7677
def __str__(self):
7778
return f'<{type(self).__name__} {self._format()}>'
7879

79-
def __class_getitem__(cls, type):
80-
return cls
80+
__class_getitem__ = classmethod(GenericAlias)
8181

8282
def _format(self):
8383
result = f'maxsize={self._maxsize!r}'

Lib/asyncio/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import types
1818
import warnings
1919
import weakref
20+
from types import GenericAlias
2021

2122
from . import base_tasks
2223
from . import coroutines
@@ -147,8 +148,7 @@ def __del__(self):
147148
self._loop.call_exception_handler(context)
148149
super().__del__()
149150

150-
def __class_getitem__(cls, type):
151-
return cls
151+
__class_getitem__ = classmethod(GenericAlias)
152152

153153
def _repr_info(self):
154154
return base_tasks._task_repr_info(self)

Lib/test/test_asyncio/test_futures.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import threading
88
import unittest
99
from unittest import mock
10-
10+
from types import GenericAlias
1111
import asyncio
1212
from asyncio import futures
1313
from test.test_asyncio import utils as test_utils
@@ -109,6 +109,11 @@ def setUp(self):
109109
self.loop = self.new_test_loop()
110110
self.addCleanup(self.loop.close)
111111

112+
def test_generic_alias(self):
113+
future = self.cls[str]
114+
self.assertEqual(future.__args__, (str,))
115+
self.assertIsInstance(future, GenericAlias)
116+
112117
def test_isfuture(self):
113118
class MyFuture:
114119
_asyncio_future_blocking = None

Lib/test/test_asyncio/test_queues.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest import mock
55

66
import asyncio
7+
from types import GenericAlias
78
from test.test_asyncio import utils as test_utils
89

910

@@ -92,6 +93,11 @@ def test_repr(self):
9293
def test_str(self):
9394
self._test_repr_or_str(str, False)
9495

96+
def test_generic_alias(self):
97+
q = asyncio.Queue[int]
98+
self.assertEqual(q.__args__, (int,))
99+
self.assertIsInstance(q, GenericAlias)
100+
95101
def test_empty(self):
96102
with self.assertWarns(DeprecationWarning):
97103
q = asyncio.Queue(loop=self.loop)

Lib/test/test_asyncio/test_tasks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import random
1010
import re
1111
import sys
12+
import types
1213
import textwrap
1314
import traceback
14-
import types
1515
import unittest
1616
import weakref
1717
from unittest import mock
18+
from types import GenericAlias
1819

1920
import asyncio
2021
from asyncio import coroutines
@@ -120,6 +121,12 @@ def setUp(self):
120121
self.loop.set_task_factory(self.new_task)
121122
self.loop.create_future = lambda: self.new_future(self.loop)
122123

124+
125+
def test_generic_alias(self):
126+
task = self.__class__.Task[str]
127+
self.assertEqual(task.__args__, (str,))
128+
self.assertIsInstance(task, GenericAlias)
129+
123130
def test_task_cancel_message_getter(self):
124131
async def coro():
125132
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`asyncio` generic classes now return :class:`types.GenericAlias` in ``__class_getitem__`` instead of the same class.

Modules/_asynciomodule.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,13 +1474,6 @@ FutureObj_finalize(FutureObj *fut)
14741474
PyErr_Restore(error_type, error_value, error_traceback);
14751475
}
14761476

1477-
static PyObject *
1478-
future_cls_getitem(PyObject *cls, PyObject *type)
1479-
{
1480-
Py_INCREF(cls);
1481-
return cls;
1482-
}
1483-
14841477
static PyAsyncMethods FutureType_as_async = {
14851478
(unaryfunc)future_new_iter, /* am_await */
14861479
0, /* am_aiter */
@@ -1500,7 +1493,7 @@ static PyMethodDef FutureType_methods[] = {
15001493
_ASYNCIO_FUTURE_GET_LOOP_METHODDEF
15011494
_ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF
15021495
_ASYNCIO_FUTURE__REPR_INFO_METHODDEF
1503-
{"__class_getitem__", future_cls_getitem, METH_O|METH_CLASS, NULL},
1496+
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
15041497
{NULL, NULL} /* Sentinel */
15051498
};
15061499

@@ -2487,13 +2480,6 @@ TaskObj_finalize(TaskObj *task)
24872480
FutureObj_finalize((FutureObj*)task);
24882481
}
24892482

2490-
static PyObject *
2491-
task_cls_getitem(PyObject *cls, PyObject *type)
2492-
{
2493-
Py_INCREF(cls);
2494-
return cls;
2495-
}
2496-
24972483
static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */
24982484

24992485
static PyMethodDef TaskType_methods[] = {
@@ -2513,7 +2499,7 @@ static PyMethodDef TaskType_methods[] = {
25132499
_ASYNCIO_TASK_GET_NAME_METHODDEF
25142500
_ASYNCIO_TASK_SET_NAME_METHODDEF
25152501
_ASYNCIO_TASK_GET_CORO_METHODDEF
2516-
{"__class_getitem__", task_cls_getitem, METH_O|METH_CLASS, NULL},
2502+
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
25172503
{NULL, NULL} /* Sentinel */
25182504
};
25192505

0 commit comments

Comments
 (0)