From 15d052a7f49b9614564a0eabd5cc0979318a0dae Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 22 Jan 2022 10:29:36 +0530 Subject: [PATCH 1/3] bpo-46469: Make asyncio generic classes return GenericAlias --- Lib/asyncio/futures.py | 4 ++-- Lib/asyncio/queues.py | 4 ++-- Lib/asyncio/tasks.py | 4 ++-- Lib/test/test_asyncio/test_futures.py | 7 ++++++- Lib/test/test_asyncio/test_queues.py | 8 ++++++-- Lib/test/test_asyncio/test_tasks.py | 8 +++++++- Modules/_asynciomodule.c | 18 ++---------------- 7 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 10f8f0554e4b60..8e8cd87612588c 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -8,6 +8,7 @@ import contextvars import logging import sys +from types import GenericAlias from . import base_futures from . import events @@ -106,8 +107,7 @@ def __del__(self): context['source_traceback'] = self._source_traceback self._loop.call_exception_handler(context) - def __class_getitem__(cls, type): - return cls + __class_getitem__ = classmethod(GenericAlias) @property def _log_traceback(self): diff --git a/Lib/asyncio/queues.py b/Lib/asyncio/queues.py index a87ec8b2158767..10dd689bbb2efa 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -2,6 +2,7 @@ import collections import heapq +from types import GenericAlias from . import locks from . import mixins @@ -69,8 +70,7 @@ def __repr__(self): def __str__(self): return f'<{type(self).__name__} {self._format()}>' - def __class_getitem__(cls, type): - return cls + __class_getitem__ = classmethod(GenericAlias) def _format(self): result = f'maxsize={self._maxsize!r}' diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 53eef84427be10..445a9f51226ed7 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -17,6 +17,7 @@ import types import warnings import weakref +from types import GenericAlias from . import base_tasks from . import coroutines @@ -123,8 +124,7 @@ def __del__(self): self._loop.call_exception_handler(context) super().__del__() - def __class_getitem__(cls, type): - return cls + __class_getitem__ = classmethod(GenericAlias) def _repr_info(self): return base_tasks._task_repr_info(self) diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 95983f05508071..84d7d45af949ef 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -7,7 +7,7 @@ import threading import unittest from unittest import mock - +from types import GenericAlias import asyncio from asyncio import futures from test.test_asyncio import utils as test_utils @@ -109,6 +109,11 @@ def setUp(self): self.loop = self.new_test_loop() self.addCleanup(self.loop.close) + def test_generic_alias(self): + future = self.cls[str] + self.assertEqual(future.__args__, (str,)) + self.assertIsInstance(future, GenericAlias) + def test_isfuture(self): class MyFuture: _asyncio_future_blocking = None diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py index 63a9a5f270cc92..b1a53b859c5ccb 100644 --- a/Lib/test/test_asyncio/test_queues.py +++ b/Lib/test/test_asyncio/test_queues.py @@ -1,9 +1,8 @@ """Tests for queues.py""" import unittest -from unittest import mock - import asyncio +from types import GenericAlias from test.test_asyncio import utils as test_utils @@ -74,6 +73,11 @@ def test_repr(self): def test_str(self): self._test_repr_or_str(str, False) + def test_generic_alias(self): + q = asyncio.Queue[int] + self.assertEqual(q.__args__, (int,)) + self.assertIsInstance(q, GenericAlias) + def test_empty(self): q = asyncio.Queue() self.assertTrue(q.empty()) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 832ff80f115ca6..8c4dceacdeec96 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -11,10 +11,10 @@ import sys import textwrap import traceback -import types import unittest import weakref from unittest import mock +from types import GenericAlias import asyncio from asyncio import coroutines @@ -108,6 +108,12 @@ def setUp(self): self.loop.set_task_factory(self.new_task) self.loop.create_future = lambda: self.new_future(self.loop) + + def test_generic_alias(self): + task = self.__class__.Task[str] + self.assertEqual(task.__args__, (str,)) + self.assertIsInstance(task, GenericAlias) + def test_task_cancel_message_getter(self): async def coro(): pass diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index b08a7d1c024c37..2216dd0178173a 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1480,13 +1480,6 @@ 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 */ 0, /* am_aiter */ @@ -1507,7 +1500,7 @@ static PyMethodDef FutureType_methods[] = { _ASYNCIO_FUTURE_GET_LOOP_METHODDEF _ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF _ASYNCIO_FUTURE__REPR_INFO_METHODDEF - {"__class_getitem__", future_cls_getitem, METH_O|METH_CLASS, NULL}, + {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* Sentinel */ }; @@ -2449,13 +2442,6 @@ 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[] = { @@ -2475,7 +2461,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}, + {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {NULL, NULL} /* Sentinel */ }; From 58eb242f16c9c68f5c2f95e498e0574f7935ba32 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 22 Jan 2022 05:05:09 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst diff --git a/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst b/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst new file mode 100644 index 00000000000000..762ef2facaedcb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst @@ -0,0 +1 @@ +Fix :mod:`asyncio` generic classes return :class:`types.GenericAlias` in ``__class_getitem__`` instead of the same class. \ No newline at end of file From fee6d090ca0b858caf1be9f67b5b2285b0090f5b Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sat, 22 Jan 2022 14:16:10 +0530 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst Co-authored-by: Jelle Zijlstra --- .../next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst b/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst index 762ef2facaedcb..0d0e4b5d3d7358 100644 --- a/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst +++ b/Misc/NEWS.d/next/Library/2022-01-22-05-05-08.bpo-46469.plUab5.rst @@ -1 +1 @@ -Fix :mod:`asyncio` generic classes return :class:`types.GenericAlias` in ``__class_getitem__`` instead of the same class. \ No newline at end of file +:mod:`asyncio` generic classes now return :class:`types.GenericAlias` in ``__class_getitem__`` instead of the same class. \ No newline at end of file