8000 bpo-46469: Make asyncio generic classes return GenericAlias by kumaraditya303 · Pull Request #30777 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46469: Make asyncio generic classes return GenericAlias #30777

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 3 commits into from
Jan 22, 2022
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
4 changes: 2 additions & 2 deletions Lib/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import contextvars
import logging
import sys
from types import GenericAlias

from . import base_futures
from . import events
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import collections
import heapq
from types import GenericAlias

from . import locks
from . import mixins
Expand Down Expand Up @@ -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}'
Expand Down
4 changes: 2 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import types
import warnings
import weakref
from types import GenericAlias

from . import base_tasks
from . import coroutines
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_asyncio/test_queues.py
8000
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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())
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`asyncio` generic classes now return :class:`types.GenericAlias` in ``__class_getitem__`` instead of the same class.
18 changes: 2 additions & 16 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
};

Expand Down Expand Up @@ -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[] = {
Expand All @@ -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 */
};

Expand Down
0