fix: correct sync iterator type of asyncio.as_completed to yield Coroutine#15672
Open
armorbreak001 wants to merge 2 commits intopython:mainfrom
Open
fix: correct sync iterator type of asyncio.as_completed to yield Coroutine#15672armorbreak001 wants to merge 2 commits intopython:mainfrom
armorbreak001 wants to merge 2 commits intopython:mainfrom
Conversation
…utine When as_completed() is iterated with a regular for loop (not async for), it yields coroutines, not Futures. The previous stub incorrectly typed the sync iterator path as Iterator[Future[_T]] which would allow calling .result() without a type error even though that crashes at runtime with AttributeError. Changes: - 3.13+: _SyncAndAsyncIterator now extends Iterator[Coroutine[Any, Any, _T]] (sync) + AsyncIterator[Future[_T]] (async) - 3.10-3.12: Return type changed to Iterator[Coroutine[Any, Any, _T]] - <3.10: Same correction for legacy loop= variant Fixes python#15646
This comment has been minimized.
This comment has been minimized.
Revert changes to <3.13 stubs to avoid breaking existing code that assigns as_completed() sync-iter results to Future/Task variables. The Coroutine type is correct at runtime but too breaking for older versions. Only 3.13+ gets the fix where _SyncAndAsyncIterator Protocol exists.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
- pandas/core/computation/ops.py:328: error: Need type annotation for "_binary_ops_dict" (hint: "_binary_ops_dict: dict[<type>, <type>] = ...") [var-annotated]
|
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.
Summary
Fixes #15646 —
as_completed()sync iteration yieldsCoroutine, notFuture.Problem
When iterating over
asyncio.as_completed()with a regularforloop (notasync for), it yields coroutines at runtime. However, the stub typed the sync iterator asIterator[Future[_T]], which allows calling.result()without a type error — but this crashes at runtime withAttributeError: 'coroutine' object has no attribute 'result'.Confirmed on CPython 3.12:
Changes
stdlib/asyncio/tasks.pyi:_SyncAndAsyncIterator[Future[_T]](sync=Iterator[Future])_SyncAndAsyncIterator[_T]where sync path isIterator[Coroutine[Any, Any, _T]]Iterator[Future[_T]]Iterator[Future[_T]](with loop=)The 3.13+
_SyncAndAsyncIteratorProtocol now correctly reflects:__iter__): yieldsCoroutine[Any, Any, T]__aiter__): yieldsFuture[T]Scope Limitation
The fix is limited to Python 3.13+ only. While the same runtime behavior exists in older versions, changing those stubs breaks existing code (confirmed via mypy_primer) that assigns sync-iter results to
Future/Taskannotated variables — even though such code would crash at runtime when calling.result(). The typeshed team can decide whether to accept that breakage in a future major version bump.Verification
iter(as_completed(...)).__next__()returns coroutine