Repository navigation
Keep spies calling through after spy.reset_mock(side_effect=True) - #647
Open
saifmodan2006 wants to merge 1 commit into
Open
saifmodan2006 wants to merge 1 commit into
saifmodan2006 wants to merge 1 commit into
Conversation
…test-dev#636) Spies call the spied code from their side_effect. For spies whose mock is a MagicMock (classmethods, staticmethods, builtins and classes), calling spy.reset_mock(side_effect=True) cleared that side_effect, causing subsequent calls to return a MagicMock rather than calling through. Override reset_mock on spy objects so that resetting side_effect restores the wrapped function and recorded spy attributes are reset.
saifmodan2006
force-pushed
the
fix/spy-reset-mock-side-effect
branch
from
October 17, 2026 18:16
3153fb0 to
fd4fe26
Compare
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.
Problem
For spies whose mock is a
MagicMock(classmethods,staticmethods, builtins and classes), callingspy.reset_mock(side_effect=True)clears the mock'sside_effect. Because the spy relies onside_effectto invoke the wrapped implementation, subsequent calls return aMagicMockrather than calling through to the spied code (#636).Solution
Override
reset_mockon spy objects returned bymocker.spy:reset_mock(*args, **kwargs)to reset mock call history.spy_return,spy_return_list, etc.).spy_obj.side_effect = wrappedwhenside_effect=Trueis passed, restoring the spied code invocation.Validation
tests/test_pytest_mock.pycoveringclassmethod,staticmethod,builtin, and class spies afterspy.reset_mock(return_value=..., side_effect=...).side_effectoverride on a spy is cleared byspy.reset_mock(side_effect=True).spy.reset_mock().CHANGELOG.rstand updateddocs/usage.rst.Closes #636