python / python/cpython

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

未关闭
#138,097 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接文档 URL 中 Python 表达式参考的“Generator expressions”部分开始,并将其中对异步生成器表达式的定义与报告的嵌套异步列表推导式示例进行比较。明确外层生成器表达式何时会变为异步,包括它与包含异步生成器表达式的外层表达式之间的区别。在继续之前,检查相关的 CPython issue gh-138105。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。