python / python/cpython

Allow Return Statements with Values in Asynchronous Generators

未關閉
#125,400 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

Proposal:
async def gen():
    yield 1
    yield 2
    return 3


async def runner():
    g = gen()
    while True:
        try:
            a = await g.__anext__()
            print("yielded", a)
        except StopAsyncIteration as e:
            print("returned", e.value)
            break


asyncio.run(runner())


# yielded 1
# yielded 2
# returned 3

The proposal adds optional return values to async generators.

This feature should be implemented to achieve syntactic and functional parity with synchronous generators.

Today:

def gen():
  yield 1
  yield 2
  return 3

async def agen():
  yield 1
  yield 2
  return 3 # Syntax error

Proposal:

def gen():
  yield 1
  yield 2
  return 3

async def agen():
  yield 1
  yield 2
  return 3

It can be implemented trivially in CPython as a nonbreaking change.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/allow-return-statements-with-values-in-asynchronous-generators/66886

Linked PRs
  • gh-125401

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先閱讀連結的 Discourse 討論和連結的 PR gh-125401,以了解提議的語言和執行階段變更。使用 issue 中的非同步產生器範例作為行為參考;完成的標準是非同步產生器可以回傳一個值,並且可以透過 StopAsyncIteration 觀察到該值,同時不破壞現有行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
20/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。