Skip to content

Clarify that generator containing an async list comprehension becomes an async generator #138097

@injust

Description

@injust

Bug report

Bug description:

import asyncio


async def foo():
    print(x for x in [1])  # <generator object foo.<locals>.<genexpr> at 0x...>
    print([a async for a in b] for x in [1])  # <async_generator object foo.<locals>.<genexpr> at 0x...>


asyncio.run(foo())

This happens only if the genexp contains an async listcomp inside of it:

[snip]

This makes sense because the inner listcomp is async and therefore must be evaluated within an async context. It's not clearly documented, though: https://docs.python.org/3.14/reference/expressions.html#generator-expressions says "If a generator expression contains either async for clauses or await expressions it is called an asynchronous generator expression. An asynchronous generator expression returns a new asynchronous generator object, which is an asynchronous iterator (see Asynchronous Iterators)." However, I wouldn't necessarily expect that to include async for expressions in a nested scope. Also, when a genexp includes an async genexp, the outer genexp doesn't become async.

So I'd recommend that you open a CPython bug report to have this clarified in the documentation.

Originally posted by @JelleZijlstra in microsoft/pyright#10827 (reply in thread)

CPython versions tested on:

3.13

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions