Fix duplicate_iterators for async spies - #604
Open
lprnmns wants to merge 2 commits into
Open
Conversation
lprnmns
marked this pull request as ready for review
August 31, 2026 13:02
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.
Problem
mocker.spy supports async functions and documents spy_return_iter for iterator returns when duplicate_iterators=True. The synchronous spy wrapper honors that option, but the async wrapper returned the original iterator without creating the duplicate, leaving spy_return_iter as None.
Fix
Apply the existing itertools.tee handling in the async spy wrapper when duplicate_iterators=True and the awaited result is an iterator.
The unreleased changelog now records the behavior correction.
Tests
Compatibility
The change affects only the existing opt-in duplicate_iterators=True path for async functions. Non-iterable async results and duplicate_iterators=False retain their prior behavior.
Related issue
Independent reproduction; no issue is claimed. Open issue #519 concerns a separate Python-version-specific unittest.mock autospec behavior and was not reproduced on the available Python 3.12 runtime.