Issue 17826: Setting a side_effect on mock from create_autospec doesn't work (original) (raw)

Created on 2013-04-24 10:00 by michael.foord, last changed 2022-04-11 14:57 by admin. 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 "", line 1, in File "", 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) (Python triager) 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
2022-04-11 14:57:44 admin set github: 62026
2014-04-14 20:10:47 michael.foord set status: open -> closedresolution: fixedstage: commit review -> resolved
2014-04-14 20:10:17 python-dev set nosy: + python-devmessages: +
2014-04-14 18:38:01 kushal.das set files: + issue17826_v3.patchmessages: + versions: + Python 3.3, - Python 3.5
2014-04-14 15:39:04 michael.foord set messages: +
2014-04-11 06:17:42 eric.araujo set versions: + Python 3.5, - Python 3.3nosy: + eric.araujomessages: + stage: patch review -> commit review
2013-04-29 05:06:47 kushal.das set files: + issue17826_v2.patchmessages: +
2013-04-27 16:51:22 ezio.melotti set nosy: + ezio.melottistage: needs patch -> patch review
2013-04-26 12:45:40 kushal.das set files: + issue17826_v1.patchkeywords: + patchmessages: +
2013-04-26 12:40:31 kushal.das set nosy: + kushal.dasmessages: +
2013-04-26 11:35:13 michael.foord set messages: +
2013-04-24 19:20:13 gregory.p.smith set nosy: + gregory.p.smith
2013-04-24 10:00:08 michael.foord create