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.

Author Lukas Anzinger
Recipients John Villalovos, Lukas Anzinger, ezio.melotti, michael.foord, rbcollins
Date 2017-11-15.15:29:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1510759777.14.0.213398074469.issue31807@psf.upfronthosting.co.za>
In-reply-to
Content
I can reproduce the problem and have analyzed it a bit. My use case is a bit different, I want to use autospec=True and wraps= so that I can mock unbound methods but return the result from the original method (see also https://docs.python.org/3/library/unittest.mock-examples.html#mocking-unbound-methods).

The problem in the mock code is that mock.return_value actually calls __get_return_value() which replaces the actual return value mock.DEFAULT (which is stored in self._mock_return_value) with a new child mock. When the mock is then called and _mock_call() is executed, the wrapped function is not executed because self._mock_return_value is not mock.DEFAULT anymore:


    if (self._mock_wraps is not None and
         self._mock_return_value is DEFAULT):
        return self._mock_wraps(*args, **kwargs)
    if ret_val is DEFAULT:
        ret_val = self.return_value
    return ret_val


Since self._mock_return_value is not DEFAULT anymore it doesn't matter if the mock wraps something and it will instead just return a new mock.

I think that the side effect of the assignment to self.return_value in _setup_func() is not intentional, i.e. the child mock should actually be created only if there is an outside access to return_value, but not when it is just wrapped in a function. The assignment can be made side effect free by assigning the internal attribute _mock_return_value (see attached patch). This solves the problem for me.
 
I've attached a patch that fixes the problem and doesn't seem to introduce a regression (all unittest.mock tests pass).

If somebody is interested in getting this merged, I'm happy to provide a regression test and everything else that is needed to get this merged.

Cheers,

Lukas
History
Date User Action Args
2017-11-15 15:29:37Lukas Anzingersetrecipients: + Lukas Anzinger, rbcollins, ezio.melotti, michael.foord, John Villalovos
2017-11-15 15:29:37Lukas Anzingersetmessageid: <1510759777.14.0.213398074469.issue31807@psf.upfronthosting.co.za>
2017-11-15 15:29:37Lukas Anzingerlinkissue31807 messages
2017-11-15 15:29:37Lukas Anzingercreate