Allow Return Statements with Values in Asynchronous Generators
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the linked Discourse discussion and linked PR gh-125401 to understand the proposed language and runtime changes. Use the async-generator example in the issue as the behavioral reference; done means an async generator can return a value and that value is observable through StopAsyncIteration without breaking existing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 20/100