gh-150484: Fix mock_open __exit__ with contextlib.ExitStack#150521
Open
factnn wants to merge 2 commits into
Open
gh-150484: Fix mock_open __exit__ with contextlib.ExitStack#150521factnn wants to merge 2 commits into
factnn wants to merge 2 commits into
Conversation
mock_open's _exit_side_effect had a fixed 3-arg signature, but contextlib.ExitStack calls __exit__ with 4 args (self + 3 exc info). Use *args to accept any number of arguments. Fixes python#150484
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
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 gh-150484:
mock_open()'s__exit__raisesTypeErrorwhen used withcontextlib.ExitStack.Root cause:
_exit_side_effecthad a fixed 3-arg signature(exctype, excinst, exctb), butExitStack.__exit__calls the managed context's__exit__with 4 args(self, exctype, excinst, exctb). The mock's side_effect mechanism forwards all args, causing the mismatch.Fix: Change
_exit_side_effect(exctype, excinst, exctb)to_exit_side_effect(*args). The function body only callshandle.close()and doesn't use the arguments.Changes
Lib/unittest/mock.py: 1-line signature fixLib/test/test_unittest/testmock/testmock.py: regression testAI Assistance
This PR was developed with AI assistance (Claude). All changed lines have been reviewed by the human submitter.