-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Remove typing.AwaitableGenerator #13804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't know the background, but mypy makes use of this, e.g. here https://github.com/python/mypy/blob/749f2584da9425173d68eb220db7e92aa13ad8ea/mypy/checkexpr.py#L6361-L6382 So I would say it's intentional :) |
|
I have a proposal for typing generator-based coroutines. When using This is consistent with its runtime type Some drawbacks: from __future__ import annotations
import types
import asyncio
from collections.abc import Generator
from typing import Any
@types.coroutine
def generator_a(): # () -> Generator[None, Any, None]
yield None
# @types.coroutine
def generator_b() -> Generator[Any, Any, None]:
yield from asyncio.sleep(1) # type: ignore[misc]
# () -> AwaitableGenerator[Any, Any, None, Any]
def generator_c():
yield from asyncio.sleep(1)
async def test_awaitable() -> None:
await generator_a() # ok
await generator_b() # TypeError! without @coroutine
await generator_c() # bad: no @coroutine mark Taking advantage of the topic: def isgeneratorcoroutine(o: object) -> typing.TypeGuard[types.GeneratorType[Any, Any, Any]]:
return inspect.isgenerator(o) and bool(o.gi_code.co_flags & inspect.CO_ITERABLE_COROUTINE) |
While manipulating coroutines, I came across AwaitableGenerator. Everything was going well until I noticed it had four type parameters. So, I checked the code.
Apparently, they have an additional type variable
_S
, which, to me, makes no sense since it's not used in the protocol.So, I want to know if this feature is intentional and if it has any use.
If not, I think it's appropriate to follow the
Generator[YieldT, SendT, ReturnT]
structure because it is a generator but it supportsawait
.Blocked by python/mypy#8240 and pyright.
The text was updated successfully, but these errors were encountered: