Repository navigation
Add mocker.spy_context for scoped spies - #651
Open
Gabrieljr42 wants to merge 4 commits into
Open
Gabrieljr42 wants to merge 4 commits into
Gabrieljr42 wants to merge 4 commits into
Conversation
This branch has not been deployed
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.
Fixes #118.
Add
mocker.spy_context(obj, name, duplicate_iterators=False)for a spy that lasts for awithblock:The original method is restored on exit, including when the block raises. Nested spies retain method binding and callable signatures. Invalid arguments are rejected before call tracking, including for static/class methods and nested instance spies. The context yields the usual spy object and uses the fixture's patch cache. Existing
mocker.spycalls keep their interface; the separate method follows the compatibility concern raised in the issue.Spies can be stopped with
stoporstopallinside the block. Overlapping patches must be stopped in reverse order: stopping an outer patch while an inner patch depends on it raisesRuntimeErrorbefore either registration changes. This prevents inner cleanup from restoring a spy that has already been removed.stopallhandles reverse cleanup, including multi-attribute patches.Regression cases cover normal and exceptional cleanup, nested class and instance spies, async methods, iterator tracking, descriptors, explicit stops and overlapping patches. The usage docs explain the new API and stop order.
Validation on Python 3.14.7 with pytest 9.1.1:
tox -e py314,norewrite: 190 passed with assertion rewriting; 173 passed and 17 expected skips without it. The installed plugin matches the edited source in both environments.unittest.mockand standalonemock, including three nested class scopes, descriptor restoration, multi-attribute patch overlap, signature guards and async await tracking.The changelog entry is under UNRELEASED.