Clarify that generator containing an async list comprehension becomes an async generator
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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 https://github.com/microsoft/pyright/discussions/10827#discussioncomment-14179632
CPython versions tested on:
3.13
Operating systems tested on:
No response
Linked PRs
- gh-138105
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la sezione «Generator expressions» del riferimento alle espressioni Python all’URL della documentazione indicata e confronta la sua definizione delle espressioni generatrici asincrone con l’esempio segnalato di una list-comprehension asincrona annidata. Chiarisci quando l’espressione generatrice esterna diventa asincrona, inclusa la distinzione rispetto a un’espressione esterna che contiene un’espressione generatrice asincrona. Controlla la relativa issue di CPython, gh-138105, prima di procedere.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100