Skip to content

Commit dec3672

Browse files
isidenticalmiss-islington
authored andcommitted
bpo-38978: Implement __class_getitem__ for asyncio objects (GH-17491)
https://bugs.python.org/issue38978
1 parent 723f71a commit dec3672

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

Lib/asyncio/futures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def __del__(self):
103103
context['source_traceback'] = self._source_traceback
104104
self._loop.call_exception_handler(context)
105105

106+
def __class_getitem__(cls, type):
107+
return cls
108+
106109
@property
107110
def _log_traceback(self):
108111
return self.__log_traceback

Lib/asyncio/queues.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def __repr__(self):
7676
def __str__(self):
7777
return f'<{type(self).__name__} {self._format()}>'
7878

79+
def __class_getitem__(cls, type):
80+
return cls
81+
7982
def _format(self):
8083
result = f'maxsize={self._maxsize!r}'
8184
if getattr(self, '_queue', None):

Lib/asyncio/tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def __del__(self):
175175
self._loop.call_exception_handler(context)
176176
super().__del__()
177177

178+
def __class_getitem__(cls, type):
179+
return cls
180+
178181
def _repr_info(self):
179182
return base_tasks._task_repr_info(self)
180183

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Implement ``__class_getitem__`` on asyncio objects (Future, Task, Queue).
2+
Patch by Batuhan Taskaya.

Modules/_asynciomodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,12 @@ FutureObj_finalize(FutureObj *fut)
13811381
PyErr_Restore(error_type, error_value, error_traceback);
13821382
}
13831383

1384+
static PyObject *
1385+
future_cls_getitem(PyObject *cls, PyObject *type)
1386+
{
1387+
Py_INCREF(cls);
1388+
return cls;
1389+
}
13841390

13851391
static PyAsyncMethods FutureType_as_async = {
13861392
(unaryfunc)future_new_iter, /* am_await */
@@ -1400,6 +1406,7 @@ static PyMethodDef FutureType_methods[] = {
14001406
_ASYNCIO_FUTURE_DONE_METHODDEF
14011407
_ASYNCIO_FUTURE_GET_LOOP_METHODDEF
14021408
_ASYNCIO_FUTURE__REPR_INFO_METHODDEF
1409+
{"__class_getitem__", future_cls_getitem, METH_O|METH_CLASS, NULL},
14031410
{NULL, NULL} /* Sentinel */
14041411
};
14051412

@@ -2429,6 +2436,13 @@ TaskObj_finalize(TaskObj *task)
24292436
FutureObj_finalize((FutureObj*)task);
24302437
}
24312438

2439+
static PyObject *
2440+
task_cls_getitem(PyObject *cls, PyObject *type)
2441+
{
2442+
Py_INCREF(cls);
2443+
return cls;
2444+
}
2445+
24322446
static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */
24332447

24342448
static PyMethodDef TaskType_methods[] = {
@@ -2449,6 +2463,7 @@ static PyMethodDef TaskType_methods[] = {
24492463
_ASYNCIO_TASK_GET_NAME_METHODDEF
24502464
_ASYNCIO_TASK_SET_NAME_METHODDEF
24512465
_ASYNCIO_TASK_GET_CORO_METHODDEF
2466+
{"__class_getitem__", task_cls_getitem, METH_O|METH_CLASS, NULL},
24522467
{NULL, NULL} /* Sentinel */
24532468
};
24542469

0 commit comments

Comments
 (0)