You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a question on adding types to a function that returns async_generator object
Here's a simple snippet that carries the issue.
async def gen() -> AsyncGenerator[int, None]:
for idx in range(100):
yield idx
async def func():
await asyncio.sleep(1)
return gen()
generator = await func()
async for idx in generator:
print(idx)
In the snippet above, what would be the return type of func?
Setting as AsyncGenerator seems weird cause you need an await keyword to get the generator.
And setting as Awaitable[AsyncGenerator] is also weird, as func itself is already a coroutine.
The reason func returns a async_coroutine object and not just iterate through it (async def func(): async for idx in gen(): ...) is that I wanted to make sure await asyncio.sleep gets called when the generator is created.
The text was updated successfully, but these errors were encountered:
I have a question on adding types to a function that returns
async_generator object
Here's a simple snippet that carries the issue.
In the snippet above, what would be the return type of
func
?Setting as
AsyncGenerator
seems weird cause you need anawait
keyword to get the generator.And setting as
Awaitable[AsyncGenerator]
is also weird, asfunc
itself is already a coroutine.The reason
func
returns aasync_coroutine object
and not just iterate through it (async def func(): async for idx in gen(): ...
) is that I wanted to make sureawait asyncio.sleep
gets called when the generator is created.The text was updated successfully, but these errors were encountered: