Skip to content

rename generator -> iterator to better reflect reality - #2240

Merged
ianhi merged 5 commits into
mainfrom
ian/async-shenanigans
Jun 29, 2026
Merged

rename generator -> iterator to better reflect reality#2240
ianhi merged 5 commits into
mainfrom
ian/async-shenanigans

Conversation

@ianhi

@ianhi ianhi commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

The typing of these methods was inaccurately declaring them as generators when they were in fact iterators. See the bottom of this table https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes. generators have asend, athrow and aclose.

This is pretty niche, but it did trick an agent into writing some broken code when I was using array_chunk_iterator to do some clever stuff with reading a virtualized file more effficietnyl. Specifically the aclose is useful in that situation to close the stream. Otherwise the iterator holds a session lock until the iterator is fully exhausted, then garbage collected. That is slower because we have to wait for the whole stream, and also leads to non-determinisitic behavior in the completion of the stream. Or it's also possible to accidentally deadlock yourself if you break out of the iter loop without finishing it. With aclose you'd have an escape hatch against that.

So this PR does three related things:

  1. Add aclose to our iterator to allow deterministic behavior on closing these streams.
    • this code is the part I am most unsure about
    • notably they are still iterators, but iterators with an extra method not full generators
  2. Renamed PyAsyncGenerator to be an PyAsyncCloseableIterator
  3. Unified the typing across all files to be accurate to what we actually return.
    -This is also where the __class_getitem__ comes from. It is necessary to allow subscripting for type annotations. We didn't need this previously because we were using the built in collections.AsyncIterator in our annotations which is not strictly accurate.

Comment thread Changelog.python.md Outdated
ianhi added 2 commits June 24, 2026 13:07
The runtime class defines __class_getitem__ (to support subscripting in
annotations), so the stub must declare it too or stubtest fails with
"not present in stub".
@ianhi
ianhi enabled auto-merge June 29, 2026 19:41
@ianhi
ianhi added this pull request to the merge queue Jun 29, 2026
Merged via the queue into main with commit abae997 Jun 29, 2026
31 checks passed
@ianhi
ianhi deleted the ian/async-shenanigans branch June 29, 2026 20:11
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