Repository navigation
Keep the context manager warning after resetall(side_effect=True) - #646
Open
Parsiffall1 wants to merge 2 commits into
Open
Parsiffall1 wants to merge 2 commits into
Parsiffall1 wants to merge 2 commits into
Conversation
resetall(side_effect=True) also resets the side_effect of child mocks, which removed the hook on __enter__ that warns about using mocks from mocker.patch and friends as context managers. Remember the original __enter__ child and its hook, and reinstall the hook after the reset, like the side_effect of spies. A deleted or replaced __enter__ is left alone. Fixes pytest-dev#637
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 #637.
mocker.patchand friends install aside_effecton the mock's__enter__that warns about using the mock as a context manager.mocker.resetall(side_effect=True)resets theside_effectof child mocks too, so after it the warning was gone:_start_patchnow records the original__enter__child and its warning hook on the cache item, andresetallputs the hook back after resetting, the same way it already restores theside_effectof spies. Because the original child is kept rather than looked up again, a__enter__the user deleted or replaced is left alone.patch.context_managernever had the hook, so nothing changes there.Tests added to
TestContextManagerWarnings:resetall(return_value=True, side_effect=True)formocker.patch;MagicMock, which still calls through;patch.context_managerstill does not warn afterresetall;resetall(side_effect=True)does not fail afterdel mocked.__enter__.The first two fail on main; the last one covers the deleted
__enter__case of the fix itself.tox -e py313,py314,norewritepasses (172 passed; norewrite 155 passed, 17 expected skips), and pre-commit passes on the changed files. A changelog entry is included.