-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
create_autospec() doesn't respect configure_mock style kwargs #90848
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
Comments
When using Lines 2693 to 2741 in 77bab59
Here's a simple test case:
|
I guess the problem is that during the initial mock creation kwargs is passed so calling test_method immediately after mock creation raises ValueError. But as we loop through the attributes and create new child mock for the attributes the original configured mock for the method that raises ValueError is overridden by another object without the configuration info. Probably it makes sense to call configure_mock again after all children mock are constructed. Something like below works and I don't see any test failures in mock related test cases. index 2719f74d6f..585e875c95 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2637,6 +2637,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
f'[object={spec!r}]')
is_async_func = _is_async_func(spec)
_kwargs = {'spec': spec}
+ original_kwargs = kwargs
if spec_set:
_kwargs = {'spec_set': spec}
elif spec is None:
@@ -2740,6 +2741,9 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if isinstance(new, FunctionTypes):
setattr(mock, entry, new)
+ if _is_instance_mock(mock):
+ mock.configure_mock(**original_kwargs)
+
return mock |
…wargs (pythonGH-118163) (cherry picked from commit b28a333) Co-authored-by: infohash <46137868+infohash@users.noreply.github.com>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: