Skip to content

Commit 2721f8a

Browse files
committed
Doc: Clarify async generator expression behavior with nested async comprehensions
1 parent 6fcac09 commit 2721f8a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Doc/reference/expressions.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ the iterable expression in the leftmost :keyword:`!for` clause, it is called an
310310
execution of the coroutine function in which it appears.
311311
See also :pep:`530`.
312312

313+
Note that this behavior extends to nested comprehensions: if a comprehension
314+
contains an asynchronous comprehension in a nested scope, the outer comprehension
315+
implicitly becomes asynchronous. This also applies to generator expressions that
316+
contain asynchronous comprehensions.
317+
313318
.. versionadded:: 3.6
314319
Asynchronous comprehensions were introduced.
315320

@@ -484,6 +489,17 @@ clauses or :keyword:`await` expressions it is called an
484489
expression returns a new asynchronous generator object,
485490
which is an asynchronous iterator (see :ref:`async-iterators`).
486491

492+
Note that a generator expression becomes an asynchronous generator expression
493+
if it contains asynchronous comprehensions in nested scopes, such as an async
494+
list comprehension within the generator expression. For example::
495+
496+
# This becomes an async generator expression
497+
([a async for a in async_iterable] for x in [1])
498+
499+
This behavior occurs because the inner async comprehension requires an
500+
asynchronous context to execute, which causes the entire generator expression
501+
to become asynchronous.
502+
487503
.. versionadded:: 3.6
488504
Asynchronous generator expressions were introduced.
489505

0 commit comments

Comments
 (0)