diff options
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/asyncio/futures.py | 4 | ||||
| -rw-r--r-- | Lib/asyncio/queues.py | 4 | ||||
| -rw-r--r-- | Lib/asyncio/tasks.py | 4 | ||||
| -rw-r--r-- | Lib/test/test_asyncio/test_futures.py | 7 | ||||
| -rw-r--r-- | Lib/test/test_asyncio/test_queues.py | 8 | ||||
| -rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 8 |
6 files changed, 25 insertions, 10 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 10f8f0554e..8e8cd87612 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -8,6 +8,7 @@ import concurrent.futures import contextvars import logging import sys +from types import GenericAlias from . import base_futures from . import events @@ -106,8 +107,7 @@ class Future: 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 a87ec8b215..10dd689bbb 100644 --- a/Lib/asyncio/queues.py +++ b/Lib/asyncio/queues.py @@ -2,6 +2,7 @@ __all__ = ('Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty') import collections import heapq +from types import GenericAlias from . import locks from . import mixins @@ -69,8 +70,7 @@ class Queue(mixins._LoopBoundMixin): 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 53eef84427..445a9f5122 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -17,6 +17,7 @@ import itertools import types import warnings import weakref +from types import GenericAlias from . import base_tasks from . import coroutines @@ -123,8 +124,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation 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 95983f0550..84d7d45af9 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -7,7 +7,7 @@ import sys 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 @@ class BaseFutureTests: 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 63a9a5f270..b1a53b859c 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 @@ class QueueBasicTests(_QueueTestBase): 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 832ff80f11..8c4dceacde 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -11,10 +11,10 @@ import re 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 @@ class BaseTaskTests: 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 |
