-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-90848: Fixed create_autospec ignoring configure_mock style kwargs #118163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
cjw296
reviewed
Apr 23, 2024
cjw296
approved these changes
May 2, 2024
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
May 2, 2024
…wargs (pythonGH-118163) (cherry picked from commit b28a333) Co-authored-by: infohash <[email protected]>
GH-118517 is a backport of this pull request to the 3.12 branch. |
SonicField
pushed a commit
to SonicField/cpython
that referenced
this pull request
May 8, 2024
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.
Fix for:
Description
All mocks can be pre-configured with
side_effect
andreturn_value
by passing them tokwargs
which are consumed by the Mock class. Butunittest.mock.create_autospec
function is ignoring kwargs so, the user has to configure them later in the code after the initialization of the mock.Test To Reproduce
Details
This is the line 👇 which is consuming
kwargs
during the initialization of the mock:cpython/Lib/unittest/mock.py
Lines 2774 to 2777 in 85f727c
Here 👇 we are overriding the
return_value
of the mock by creating a new mock on it but this is not the problem:cpython/Lib/unittest/mock.py
Lines 2793 to 2797 in 85f727c
As it is being called recursively 👆, the attributes of the specced object will also be mocked by creating child mocks 👇:
cpython/Lib/unittest/mock.py
Lines 2838 to 2841 in 85f727c
Then the child mocks are set on the recursive mock 👇:
cpython/Lib/unittest/mock.py
Lines 2849 to 2851 in 85f727c
This 👆 is the cause of the issue. We have set new child attribute mock which is not configured.
Fix Suggested by @tirkarthi
Code Changes
kwargs
with respect to the parent mock.kwargs
tochild_kwargs
because it was shadowing the name of the argument which is now being used to configure the parent mock.