[Python-checkins] bpo-38978: Implement __class_getitem__ for asyncio objects (GH-17491)
Miss Islington (bot)
webhook-mailer at python.org
Sat Dec 7 06:05:13 EST 2019
https://github.com/python/cpython/commit/dec367261e7e2bb4dd42feeb58031abed2ade683
commit: dec367261e7e2bb4dd42feeb58031abed2ade683
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-12-07T03:05:07-08:00
summary:
bpo-38978: Implement __class_getitem__ for asyncio objects (GH-17491)
https://bugs.python.org/issue38978
files:
A Misc/NEWS.d/next/Library/2019-12-07-13-40-52.bpo-38978.R3gHZI.rst
M Lib/asyncio/futures.py
M Lib/asyncio/queues.py
M Lib/asyncio/tasks.py
M Modules/_asynciomodule.c
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index 9afda220bd78f..a3cf379ee8170 100644
--- a/Lib/asyncio/futures.py
+++ b/Lib/asyncio/futures.py
@@ -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
diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py
index 390ae9a6821c4..cd3f7c6a56789 100644
--- a/Lib/asyncio/queues.py
+++ b/Lib/asyncio/queues.py
@@ -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):
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 38d982716d46a..894d28eb107ac 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2019-12-07-13-40-52.bpo-38978.R3gHZI.rst b/Misc/NEWS.d/next/Library/2019-12-07-13-40-52.bpo-38978.R3gHZI.rst
new file mode 100644
index 0000000000000..8b2eab0d52acf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-07-13-40-52.bpo-38978.R3gHZI.rst
@@ -0,0 +1,2 @@
+Implement ``__class_getitem__`` on asyncio objects (Future, Task, Queue).
+Patch by Batuhan Taskaya.
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index aa46e3cf5640f..2d147447ab787 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -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 */
@@ -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 */
};
@@ -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[] = {
@@ -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 */
};
More information about the Python-checkins
mailing list