classification
Title: Setting a side_effect on mock from create_autospec doesn't work
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: eric.araujo, ezio.melotti, gregory.p.smith, kushal.das, michael.foord, python-dev
Priority: normal Keywords: patch

Created on 2013-04-24 10:00 by michael.foord, last changed 2014-04-14 20:10 by michael.foord. This issue is now closed.

Files
File name Uploaded Description Edit
issue17826_v1.patch kushal.das, 2013-04-26 12:45 Code and test case update review
issue17826_v2.patch kushal.das, 2013-04-29 05:06 Code and test case update revision 2 review
issue17826_v3.patch kushal.das, 2014-04-14 18:38 New patch with fix in proper place for side_effect for functions (includes test case). review
Messages (9)
msg187692 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2013-04-24 10:00
>>> from unittest.mock import create_autospec
>>> def f(): pass
... 
>>> m = create_autospec(f)
>>> m.side_effect = [1, 2]
>>> m()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3, in f
  File "/compile/py3k-cpython/Lib/unittest/mock.py", line 872, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/compile/py3k-cpython/Lib/unittest/mock.py", line 931, in _mock_call
    result = next(effect)
TypeError: 'list' object is not an iterator
msg187840 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2013-04-26 11:35
This illustrates the difference:

>>> from mock import Mock, create_autospec
>>> some_list = [1, 2, 3]
>>> m = Mock()
>>> m.side_effect = some_list
>>> m.side_effect
<listiterator object at 0x1004ab7d0>
>>> m2 = create_autospec(lambda: None)
>>> m2.side_effect = some_list
>>> m2.side_effect
[1, 2, 3]


When setting a side_effect on a function (created by create_autospec) the side_effect setter is not used - so turning the list into an iterator needs to be done on first use instead.
msg187844 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2013-04-26 12:40
Working on this.
msg187845 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2013-04-26 12:45
Patch along with a test for the same.
msg188036 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2013-04-29 05:06
Version 2 of the patch, with typo fixed also one more addition test to check callable side effect in create_autospec.
msg215916 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2014-04-11 06:17
Michael, a patch including tests is ready for this issue.
msg216098 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-04-14 15:39
Can you explain why we need to check for the call_count here? I don't understand why this is needed.
msg216164 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2014-04-14 18:38
New patch with fix in proper place for side_effect for functions (includes test case).
msg216196 - (view) Author: Roundup Robot (python-dev) Date: 2014-04-14 20:10
New changeset 1e3c64470629 by Michael Foord in branch '3.4':
Issue 17826. Setting an iterable side_effect on a mock created by create_autospec now works
http://hg.python.org/cpython/rev/1e3c64470629
History
Date User Action Args
2014-04-14 20:10:47michael.foordsetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2014-04-14 20:10:17python-devsetnosy: + python-dev
messages: + msg216196
2014-04-14 18:38:01kushal.dassetfiles: + issue17826_v3.patch

messages: + msg216164
versions: + Python 3.3, - Python 3.5
2014-04-14 15:39:04michael.foordsetmessages: + msg216098
2014-04-11 06:17:42eric.araujosetversions: + Python 3.5, - Python 3.3
nosy: + eric.araujo

messages: + msg215916

stage: patch review -> commit review
2013-04-29 05:06:47kushal.dassetfiles: + issue17826_v2.patch

messages: + msg188036
2013-04-27 16:51:22ezio.melottisetnosy: + ezio.melotti

stage: needs patch -> patch review
2013-04-26 12:45:40kushal.dassetfiles: + issue17826_v1.patch
keywords: + patch
messages: + msg187845
2013-04-26 12:40:31kushal.dassetnosy: + kushal.das
messages: + msg187844
2013-04-26 11:35:13michael.foordsetmessages: + msg187840
2013-04-24 19:20:13gregory.p.smithsetnosy: + gregory.p.smith
2013-04-24 10:00:08michael.foordcreate