From 14e0a8348e044dba11180cf7f0786844e78953f7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 3 Jan 2025 09:09:06 +0000 Subject: [PATCH] get demo running --- taskgroup/install.py | 13 ++++++++---- taskgroup/runners.py | 2 +- taskgroup/taskgroups.py | 14 ++++++++----- taskgroup/tasks.py | 46 ++++++++++++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/taskgroup/install.py b/taskgroup/install.py index b1259bc..a620a72 100644 --- a/taskgroup/install.py +++ b/taskgroup/install.py @@ -1,6 +1,6 @@ +import sys import contextvars import asyncio -import collections.abc import contextlib import types from typing import cast @@ -9,6 +9,11 @@ from typing_extensions import Self, TypeVar +if sys.version_info >= (3, 9): + from collections.abc import Generator, Coroutine +else: + from typing import Generator, Coroutine + UNCANCEL_DONE = object() @@ -49,12 +54,12 @@ def _async_yield(v): class WrapCoro( - collections.abc.Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], - collections.abc.Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], + Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], + Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], ): def __init__( self, - coro: collections.abc.Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], + coro: Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], context: contextvars.Context, ): self._coro = coro diff --git a/taskgroup/runners.py b/taskgroup/runners.py index 64c66c0..7ccd0e1 100644 --- a/taskgroup/runners.py +++ b/taskgroup/runners.py @@ -95,7 +95,7 @@ def close(self) -> None: loop.run_until_complete( loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT) # type: ignore ) - else: + elif sys.version_info >= (3, 9): loop.run_until_complete(loop.shutdown_default_executor()) finally: if self._set_event_loop: diff --git a/taskgroup/taskgroups.py b/taskgroup/taskgroups.py index 3c71a29..b415b4b 100644 --- a/taskgroup/taskgroups.py +++ b/taskgroup/taskgroups.py @@ -7,7 +7,6 @@ __all__ = ["TaskGroup"] import sys -import collections.abc from types import TracebackType from asyncio import events from asyncio import exceptions @@ -24,6 +23,11 @@ from typing_extensions import Self, TypeAlias, Literal, TypeVar import contextlib +if sys.version_info >= (3, 9): + from collections.abc import Generator, Coroutine +else: + from typing import Generator, Coroutine + _T = TypeVar("_T") @@ -35,14 +39,14 @@ _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) -_TaskYieldType: TypeAlias = Optional[futures.Future[object]] +_TaskYieldType: TypeAlias = "futures.Future[object] | None" if sys.version_info >= (3, 12): - _TaskCompatibleCoro: TypeAlias = collections.abc.Coroutine[Any, Any, _T_co] + _TaskCompatibleCoro: TypeAlias = Coroutine[Any, Any, _T_co] else: _TaskCompatibleCoro: TypeAlias = Union[ - collections.abc.Generator[_TaskYieldType, None, _T_co], - collections.abc.Coroutine[Any, Any, _T_co], + Generator[_TaskYieldType, None, _T_co], + Coroutine[Any, Any, _T_co], ] diff --git a/taskgroup/tasks.py b/taskgroup/tasks.py index 88d5d10..e31e0d9 100644 --- a/taskgroup/tasks.py +++ b/taskgroup/tasks.py @@ -1,12 +1,16 @@ from __future__ import annotations import asyncio -import collections.abc import contextvars -from typing import Any, Optional, Union +from typing import Any, Union, TYPE_CHECKING, Generic from typing_extensions import TypeAlias, TypeVar, Self import sys +if sys.version_info >= (3, 9): + from collections.abc import Generator, Coroutine, Awaitable +else: + from typing import Generator, Coroutine, Awaitable + _YieldT_co = TypeVar("_YieldT_co", covariant=True) _SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=None) _ReturnT_co = TypeVar("_ReturnT_co", covariant=True, default=None) @@ -15,26 +19,30 @@ _T = TypeVar("_T") _T_co = TypeVar("_T_co", covariant=True) -_TaskYieldType: TypeAlias = Optional[asyncio.Future[object]] +_TaskYieldType: TypeAlias = "asyncio.Future[object] | None" if sys.version_info >= (3, 12): - _TaskCompatibleCoro: TypeAlias = collections.abc.Coroutine[Any, Any, _T_co] -else: + _TaskCompatibleCoro: TypeAlias = Coroutine[Any, Any, _T_co] +if sys.version_info >= (3, 9): _TaskCompatibleCoro: TypeAlias = Union[ - collections.abc.Generator[_TaskYieldType, None, _T_co], - collections.abc.Coroutine[Any, Any, _T_co], + Generator[_TaskYieldType, None, _T_co], + Coroutine[Any, Any, _T_co], ] +else: + _TaskCompatibleCoro: TypeAlias = ( + "Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co]" + ) class _Interceptor( - collections.abc.Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], - collections.abc.Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], + Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], + Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd], ): def __init__( self, coro: ( - collections.abc.Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd] - | collections.abc.Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd] + Coroutine[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd] + | Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd] ), context: contextvars.Context, ): @@ -57,7 +65,21 @@ def close(self) -> None: super().close() -class Task(asyncio.Task[_T_co]): +if TYPE_CHECKING: + + class _Task(asyncio.Task[_T_co]): + pass + + +if sys.version_info >= (3, 8): + + class _Task(asyncio.Task, Generic[_T_co]): + pass +else: + _Task = asyncio.Task + + +class Task(_Task[_T_co]): def __init__( self, coro: _TaskCompatibleCoro[_T_co], *args, context=None, **kwargs ) -> None: