This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: create_autospec() doesn't respect configure_mock style kwargs
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, lisroach, marchant.jm, mariocj89, michael.foord, xtreak
Priority: normal Keywords:

Created on 2022-02-09 11:54 by marchant.jm, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg412898 - (view) Author: James Marchant (marchant.jm) Date: 2022-02-09 11:54
When using `create_autospec()` to create a mock object, it doesn't respect values passed through in the style described for passing mock configurations in the Mock constructor (https://docs.python.org/3.8/library/unittest.mock.html#unittest.mock.Mock.configure_mock). Instead, they seem to get discarded somewhere here (https://github.com/python/cpython/blob/77bab59c8a1f04922bb975cc4f11e5323d1d379d/Lib/unittest/mock.py#L2693-L2741).

Here's a simple test case:
```
from unittest.mock import create_autospec

class Test:
    def test_method(self):
        pass

autospec_mock = create_autospec(Test, instance=True, **{"test_method.side_effect": ValueError})

# Should throw a ValueError exception
autospec_mock.test_method()

# Assign manually
autospec_mock.test_method.side_effect = ValueError
# Throws as expected
autospec_mock.test_method()
```
msg413001 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2022-02-10 13:44
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
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90848
2022-02-10 13:44:04xtreaksetnosy: + cjw296, michael.foord, lisroach, mariocj89
messages: + msg413001
components: + Library (Lib), - Tests
2022-02-09 17:04:27xtreaksetnosy: + xtreak
2022-02-09 11:54:00marchant.jmcreate