python / python/cpython

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

オープン
#138,097 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンク先のドキュメント URL にある Python 式リファレンスの「Generator expressions」セクションから始め、非同期ジェネレーター式の定義を、報告されたネストされた非同期リスト内包表記の例と比較してください。外側のジェネレーター式がいつ非同期になるのかを、非同期ジェネレーター式を含む外側の式との違いも含めて明確にしてください。進める前に、関連する CPython issue gh-138105 を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。