Repository navigation
Keep the return value of __enter__ on patched mocks - #650
Open
mehdimirzafaruk wants to merge 2 commits into
Open
mehdimirzafaruk wants to merge 2 commits into
mehdimirzafaruk wants to merge 2 commits into
Conversation
The side effect that warns about using a mock from mocker.patch as a context manager returned None, so entering the patched object always gave None, ignoring the mock's __enter__.return_value. Return DEFAULT from it so the mock returns its return_value as usual. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
When a mock created by
mocker.patch(orpatch.object) has__enter__, pytest-mock sets a side effect on it that warns about using the mock as a context manager. That side effect is a lambda returning the result ofwarnings.warn(), which isNone, and since a side effect that returns something other thanDEFAULTreplaces the return value, entering the patched object always givesNone:With
unittest.mock.patch.objectthe same code givesconnection. This shows up when the patched object is itself the context manager (a lock, a pool, a session), where the warning message says it "can be ignored", but the configured__enter__.return_valueis silently lost too.The fix makes the warning side effect return
DEFAULT, so__enter__still warns and then returns itsreturn_valueas usual.Tests: added
TestContextManagerWarnings::test_patching_a_context_manager_object, which fails onmain(assert None is <MagicMock name='pool.__enter__()'>) and passes with the fix.tox -e py314,norewritepasses andpre-commitis clean.