From 46512118eac286d671058f2d77ffb96bd43de799 Mon Sep 17 00:00:00 2001 From: Martin Huschenbett Date: Mon, 23 Sep 2024 11:34:56 +0200 Subject: [PATCH] Return coroutine from AsyncGenerator.__anext__ (#12685) The `__anext__` method of an asynchronous generator defined using the `async def`/`yield` syntax returns an actual coroutine not just any awaitable. Let the definition of the `AsyncGenerator` protocol reflect this circumstance. See https://discuss.python.org/t/types-for-asynchronous-generators-too-general/64515 for the motivation behind this change. --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index cadd06358..784f30ad7 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -540,7 +540,7 @@ class AsyncIterator(AsyncIterable[_T_co], Protocol[_T_co]): def __aiter__(self) -> AsyncIterator[_T_co]: ... class AsyncGenerator(AsyncIterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra]): - def __anext__(self) -> Awaitable[_YieldT_co]: ... + def __anext__(self) -> Coroutine[Any, Any, _YieldT_co]: ... @abstractmethod def asend(self, value: _SendT_contra, /) -> Coroutine[Any, Any, _YieldT_co]: ... @overload