Skip to content

Commit

Permalink
Merge pull request #30 from graingert/restore-3-8-support
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert authored Jan 3, 2025
2 parents 7fffaee + 4d5ef83 commit 7f6bb2c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", pypy-3.10]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", pypy-3.10]
steps:
- uses: actions/checkout@v4
- name: Set up Python
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ a backport of asyncio.TaskGroup, asyncio.Runner and asyncio.timeout
## background

This is a backport of the TaskGroup, Runner and timeout code from
Python 3.12.8 to Python 3.9, Python 3.10 and Python 3.11.
Python 3.12.8 to Python 3.8, Python 3.9, Python 3.10 and Python 3.11.

## operation

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ classifiers = ["License :: OSI Approved :: MIT License"]
dynamic = ["version", "description"]
dependencies = ["exceptiongroup", "typing_extensions>=4.12.2,<5"]
readme = "README.md"
requires_python = ">=3.8"

[project.urls]
Home = "https://github.com/graingert/taskgroup"
13 changes: 9 additions & 4 deletions taskgroup/install.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import contextvars
import asyncio
import collections.abc
import contextlib
import types
from typing import cast
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion taskgroup/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 14 additions & 6 deletions taskgroup/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

__all__ = ["TaskGroup"]
import sys
import collections.abc
from types import TracebackType
from asyncio import events
from asyncio import exceptions
Expand All @@ -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, Awaitable
else:
from typing import Generator, Coroutine, Awaitable


_T = TypeVar("_T")

Expand All @@ -35,15 +39,19 @@

_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]
else:
_TaskCompatibleCoro: TypeAlias = Coroutine[Any, Any, _T_co]
elif 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 _TaskGroup:
Expand Down
46 changes: 34 additions & 12 deletions taskgroup/tasks.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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]
elif 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,
):
Expand All @@ -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


elif sys.version_info >= (3, 9):
_Task = asyncio.Task
else:

class _Task(asyncio.Task, Generic[_T_co]):
pass


class Task(_Task[_T_co]):
def __init__(
self, coro: _TaskCompatibleCoro[_T_co], *args, context=None, **kwargs
) -> None:
Expand Down

0 comments on commit 7f6bb2c

Please sign in to comment.