Skip to content

Preserve synchronous iteration over awaited collections - #12

Closed
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/awaited-synchronous-iteration
Closed

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/awaited-synchronous-iteration

Conversation

@EiNSTeiN-

Copy link
Copy Markdown
Contributor

I am opening this PR to preserve synchronous iteration over an awaited collection. The code below currently decompiles with the wrong iteration protocol:

async def select(read):
    result = []
    for item in await read():
        result.append(item)
    return result

The loop is currently emitted as:

    async for item in read():
        result.append(item)

The producer must be awaited once and its result then consumed as a regular iterable through __iter__. An async for instead requires the asynchronous iteration protocol and no longer performs the source's await read() operation. A producer returning an ordinary list therefore no longer works.

When the FOR_ITER handler sees an Await expression, it currently removes the await wrapper and marks the loop asynchronous. With this change, FOR_ITER always selects synchronous iteration. GET_AITER/GET_ANEXT retain their separate genuine-async path, so async for item in read() keeps its existing meaning.

The source syntax begins in Python 3.5; the repair is not limited to a recent bytecode version.

@ejfkdev

ejfkdev commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Merged via consolidated PR #28 (commit e629b6c). Your fix, fixtures, behavior cases and integration test are all included and verified together: real-world corpus 6411/6411, stdlib corpus 528/528, behavior tests green on 3.9-3.14. Thanks for the detailed reports and repros!

@ejfkdev ejfkdev closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants