]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38978: Implement __class_getitem__ for asyncio objects (GH-17491)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Sat, 7 Dec 2019 11:05:07 +0000 (14:05 +0300)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 7 Dec 2019 11:05:07 +0000 (03:05 -0800)
https://bugs.python.org/issue38978

Lib/asyncio/futures.py
Lib/asyncio/queues.py
Lib/asyncio/tasks.py
Misc/NEWS.d/next/Library/2019-12-07-13-40-52.bpo-38978.R3gHZI.rst [new file with mode: 0644]
Modules/_asynciomodule.c

index 9afda220bd78f24f514f016d5721b27698a81809..a3cf379ee81705e7fa22acc0e715571f534085ab 100644 (file)
@@ -103,6 +103,9 @@ class Future:
             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
index 390ae9a6821c4d4e3790458c1d5615f396b4fa14..cd3f7c6a56789152052a2d00143b852bca384e0e 100644 (file)
@@ -76,6 +76,9 @@ class Queue:
     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):
index 38d982716d46ad1b5b37c8d6b03185d0274eec31..894d28eb107acd7b439478152c4a78c2264f8f12 100644 (file)
@@ -175,6 +175,9 @@ class Task(futures._PyFuture):  # Inherit Python Task implementation
             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 (file)
index 0000000..8b2eab0
--- /dev/null
@@ -0,0 +1,2 @@
+Implement ``__class_getitem__`` on asyncio objects (Future, Task, Queue).
+Patch by Batuhan Taskaya.
index aa46e3cf5640fc3cac97172ed2425ae08a690900..2d147447ab787ecdaef73564bb65c98ea48ffd2e 100644 (file)
@@ -1381,6 +1381,12 @@ finally:
     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 @@ done:
     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 */
 };