Skip to content

bpo-38978: Implement __class_getitem__ for asyncio objects #17491

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 1 commit into from
Dec 7, 2019
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/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __del__(self):
context['source_traceback'] = self._source_traceback
self._loop.call_exception_handler(context)

def __class_getitem__(cls, type):
return cls

@property
def _log_traceback(self):
return self.__log_traceback
Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def __repr__(self):
def __str__(self):
return f'<{type(self).__name__} {self._format()}>'

def __class_getitem__(cls, type):
return cls

def _format(self):
result = f'maxsize={self._maxsize!r}'
if getattr(self, '_queue', None):
Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def __del__(self):
self._loop.call_exception_handler(context)
super().__del__()

def __class_getitem__(cls, type):
return cls

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement ``__class_getitem__`` on asyncio objects (Future, Task, Queue).
Patch by Batuhan Taskaya.
15 changes: 15 additions & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,12 @@ FutureObj_finalize(FutureObj *fut)
PyErr_Restore(error_type, error_value, error_traceback);
}

static PyObject *
future_cls_getitem(PyObject *cls, PyObject *type)
{
Py_INCREF(cls);
return cls;
}

static PyAsyncMethods FutureType_as_async = {
(unaryfunc)future_new_iter, /* am_await */
Expand All @@ -1400,6 +1406,7 @@ static PyMethodDef FutureType_methods[] = {
_ASYNCIO_FUTURE_DONE_METHODDEF
_ASYNCIO_FUTURE_GET_LOOP_METHODDEF
_ASYNCIO_FUTURE__REPR_INFO_METHODDEF
{"__class_getitem__", future_cls_getitem, METH_O|METH_CLASS, NULL},
{NULL, NULL} /* Sentinel */
};

Expand Down Expand Up @@ -2429,6 +2436,13 @@ TaskObj_finalize(TaskObj *task)
FutureObj_finalize((FutureObj*)task);
}

static PyObject *
task_cls_getitem(PyObject *cls, PyObject *type)
{
Py_INCREF(cls);
return cls;
}

static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */

static PyMethodDef TaskType_methods[] = {
Expand All @@ -2449,6 +2463,7 @@ static PyMethodDef TaskType_methods[] = {
_ASYNCIO_TASK_GET_NAME_METHODDEF
_ASYNCIO_TASK_SET_NAME_METHODDEF
_ASYNCIO_TASK_GET_CORO_METHODDEF
{"__class_getitem__", task_cls_getitem, METH_O|METH_CLASS, NULL},
{NULL, NULL} /* Sentinel */
};

Expand Down