From 35bddd289f8d716603c63f68b827cbcddf5ac804 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 16 Feb 2022 14:32:20 -0800 Subject: [PATCH] Add asyncio.taskgroups and new Task methods (#7240) Co-authored-by: Jelle Zijlstra --- stdlib/VERSIONS | 1 + stdlib/asyncio/__init__.pyi | 3 +++ stdlib/asyncio/taskgroups.pyi | 16 ++++++++++++++++ stdlib/asyncio/tasks.pyi | 3 +++ tests/stubtest_allowlists/py310.txt | 1 + tests/stubtest_allowlists/py36.txt | 1 + tests/stubtest_allowlists/py37.txt | 1 + tests/stubtest_allowlists/py38.txt | 1 + tests/stubtest_allowlists/py39.txt | 1 + 9 files changed, 28 insertions(+) create mode 100644 stdlib/asyncio/taskgroups.pyi diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index eceb75385..b78038f26 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -68,6 +68,7 @@ asyncio.exceptions: 3.8- asyncio.format_helpers: 3.7- asyncio.runners: 3.7- asyncio.staggered: 3.8- +asyncio.taskgroups: 3.11- asyncio.threads: 3.9- asyncio.trsock: 3.8- asyncore: 2.7- diff --git a/stdlib/asyncio/__init__.pyi b/stdlib/asyncio/__init__.pyi index e75874627..21eb4d9ef 100644 --- a/stdlib/asyncio/__init__.pyi +++ b/stdlib/asyncio/__init__.pyi @@ -107,6 +107,9 @@ if sys.version_info >= (3, 7): current_task as current_task, ) +if sys.version_info >= (3, 11): + from .taskgroups import TaskGroup as TaskGroup + DefaultEventLoopPolicy: type[AbstractEventLoopPolicy] if sys.platform == "win32": diff --git a/stdlib/asyncio/taskgroups.pyi b/stdlib/asyncio/taskgroups.pyi new file mode 100644 index 000000000..20b69fe43 --- /dev/null +++ b/stdlib/asyncio/taskgroups.pyi @@ -0,0 +1,16 @@ +# This only exists in 3.11+. See VERSIONS. + +from _typeshed import Self +from types import TracebackType +from typing import Any, Coroutine, Generator, TypeVar + +from .tasks import Task + +_T = TypeVar("_T") + +class TaskGroup: + def __init__(self, *, name: str | None = ...) -> None: ... + def get_name(self) -> str: ... + async def __aenter__(self: Self) -> Self: ... + async def __aexit__(self, et: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None) -> None: ... + def create_task(self, coro: Generator[Any, None, _T] | Coroutine[Any, Any, _T]) -> Task[_T]: ... diff --git a/stdlib/asyncio/tasks.pyi b/stdlib/asyncio/tasks.pyi index d0abe8647..2027e04b9 100644 --- a/stdlib/asyncio/tasks.pyi +++ b/stdlib/asyncio/tasks.pyi @@ -276,6 +276,9 @@ class Task(Future[_T], Generic[_T]): def cancel(self, msg: Any | None = ...) -> bool: ... else: def cancel(self) -> bool: ... + if sys.version_info >= (3, 11): + def cancelling(self) -> bool: ... + def uncancel(self) -> bool: ... if sys.version_info < (3, 9): @classmethod def current_task(cls, loop: AbstractEventLoop | None = ...) -> Task[Any] | None: ... diff --git a/tests/stubtest_allowlists/py310.txt b/tests/stubtest_allowlists/py310.txt index 826119db2..a74af2744 100644 --- a/tests/stubtest_allowlists/py310.txt +++ b/tests/stubtest_allowlists/py310.txt @@ -41,6 +41,7 @@ asyncio.Future.__init__ # Usually initialized from c object asyncio.Future._callbacks # Usually initialized from c object asyncio.futures.Future.__init__ # Usually initialized from c object asyncio.futures.Future._callbacks # Usually initialized from c object +asyncio.taskgroups # Added in Python 3.11 builtins.dict.get contextvars.Context.__init__ # Default C __init__ signature is wrong contextlib.AbstractAsyncContextManager.__class_getitem__ diff --git a/tests/stubtest_allowlists/py36.txt b/tests/stubtest_allowlists/py36.txt index 12d36153f..0760084be 100644 --- a/tests/stubtest_allowlists/py36.txt +++ b/tests/stubtest_allowlists/py36.txt @@ -13,6 +13,7 @@ asyncio.staggered # Added in Python 3.8 asyncio.threads # Added in Python 3.9 asyncio.trsock # Added in Python 3.8 asyncio.mixins # Added in Python 3.10 +asyncio.taskgroups # Added in Python 3.11 builtins.str.maketrans cmath.log collections.AsyncGenerator.ag_await diff --git a/tests/stubtest_allowlists/py37.txt b/tests/stubtest_allowlists/py37.txt index 86b5e7c6e..ce9a8ebd3 100644 --- a/tests/stubtest_allowlists/py37.txt +++ b/tests/stubtest_allowlists/py37.txt @@ -13,6 +13,7 @@ asyncio.staggered # Added in Python 3.8 asyncio.threads # Added in Python 3.9 asyncio.trsock # Added in Python 3.8 asyncio.mixins # Added in Python 3.10 +asyncio.taskgroups # Added in Python 3.11 builtins.dict.get builtins.str.maketrans cmath.log diff --git a/tests/stubtest_allowlists/py38.txt b/tests/stubtest_allowlists/py38.txt index ab417eb3e..099aa9c0a 100644 --- a/tests/stubtest_allowlists/py38.txt +++ b/tests/stubtest_allowlists/py38.txt @@ -18,6 +18,7 @@ asyncio.futures.Future.__init__ # Usually initialized from c object asyncio.futures.Future._callbacks # Usually initialized from c object asyncio.threads # Added in Python 3.9 asyncio.mixins # Added in Python 3.10 +asyncio.taskgroups # Added in Python 3.11 builtins.dict.get collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code diff --git a/tests/stubtest_allowlists/py39.txt b/tests/stubtest_allowlists/py39.txt index c0e648aed..e290f1774 100644 --- a/tests/stubtest_allowlists/py39.txt +++ b/tests/stubtest_allowlists/py39.txt @@ -20,6 +20,7 @@ asyncio.Future._callbacks # Usually initialized from c object asyncio.futures.Future.__init__ # Usually initialized from c object asyncio.futures.Future._callbacks # Usually initialized from c object asyncio.mixins # Added in Python 3.10 +asyncio.taskgroups # Added in Python 3.11 builtins.dict.get collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code