asyncio.as_completed() raises TypeError when the first supplied parameter is a generator that yields awaitables · Issue #88342 · python/cpython · GitHub
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-05-19.05:04:33.179>labels= ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library', 'expert-asyncio']
title='asyncio.as_completed() raises TypeError when the first supplied parameter is a generator that yields awaitables'updated_at=<Date2021-05-19.05:53:09.349>user='https://github.com/alexdelorenzo'
As seen in the attached as_completed_gen.py file, built-in containers like lists, and iterators over them, are accepted by as_completed() as the first parameter without raising an error.
However, as_completed() raises TypeError if it is called with an iterable of awaitables that is also a generator. There are examples of this behavior in as_completed_gen.py, but here is a short example using a generator expression in the main() coroutine function:
Running that example will raise a TypeError with this message:
TypeError: expect an iterable of futures, not generator
If we look at the first line in the body of as_completed(), we can see why this error is thrown for generators that yield awaitables:
defas_completed(fs, *, loop=None, timeout=None):
iffutures.isfuture(fs) orcoroutines.iscoroutine(fs):
raiseTypeError(f"expect an iterable of futures, not {type(fs).__name__}")
...
Because generators are coroutines, and the first condition in as_completed() is True, and TypeError gets raised:
fromasyncioimportcoroutines
# generators and generator expressions are coroutines
assert coroutines.iscoroutine(example() for _ in range(10))
Perhaps as_completed() can use inspect.isawaitable() instead, like so:
Activity
alexdelorenzo commented on May 19, 2021
According to the documentation, asyncio.as_completed() takes a positional argument, aws, as an iterable of awaitables[1]:
As seen in the attached as_completed_gen.py file, built-in containers like lists, and iterators over them, are accepted by as_completed() as the first parameter without raising an error.
However, as_completed() raises TypeError if it is called with an iterable of awaitables that is also a generator. There are examples of this behavior in as_completed_gen.py, but here is a short example using a generator expression in the main() coroutine function:
Running that example will raise a TypeError with this message:
If we look at the first line in the body of as_completed(), we can see why this error is thrown for generators that yield awaitables:
Because generators are coroutines, and the first condition in as_completed() is True, and TypeError gets raised:
Perhaps as_completed() can use inspect.isawaitable() instead, like so:
I made a pull request with that change here[2].
[1] https://docs.python.org/3/library/asyncio-task.html#asyncio.as_completed
[2] #26228
kumaraditya303 commented on Apr 19, 2023
3.12 allows generators so this is completed.
kumaraditya303 commented on Apr 19, 2023
#103626 clarifies this in doc.
GH-88342: clarify that `asyncio.as_completed` accepts generators yiel…
Merge branch 'main' into superopt