gh-64862: Add the stop_exception parameter in iter() and aiter() - #156298
Open
serhiy-storchaka wants to merge 2 commits into
Open
gh-64862: Add the stop_exception parameter in iter() and aiter()#156298serhiy-storchaka wants to merge 2 commits into
serhiy-storchaka wants to merge 2 commits into
Conversation
The created iterator stops when the callable raises the specified exception. The second parameter of iter() is now named stop_value and can be passed as a keyword argument. aiter() now accepts the same stop_value and stop_exception parameters, calling an asynchronous callable and awaiting the result. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
serhiy-storchaka
requested review from
a team,
AA-Turner,
ZeroIntensity,
emmatyping,
ericsnowcurrently,
erlend-aasland,
itamaro and
markshannon
as code owners
August 23, 2026 19:46
Documentation build overview
|
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
iter()andaiter()now accept the keyword-only stop_exception parameter -- an exception class or a tuple of exception classes which ends the iteration:Many callables report exhaustion by raising an exception instead of returning a special value, so the sentinel form cannot be used with them at all.
aiter()also gained the callable form, which it did not have before: the callable is called and its result is awaited for every__anext__()(the callable is only called when the result of__anext__()is awaited).The second parameter of
iter()is now named stop_value and can be passed by keyword. It can be omitted if stop_exception is given.stop_exception=StopIteration(StopAsyncIterationforaiter()) and an empty tuple never change the behavior, so they are normalized to "no stop exception"; such an iterator is pickled exactly as before. For other casescallable_iteratornow has__setstate__(), because the stop exception and the absence of the sentinel cannot be expressed as arguments ofiter().