classification
Title: Mock.side_effect as iterable or iterator
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mjpieters, r.david.murray
Priority: normal Keywords:

Created on 2015-08-13 07:35 by mjpieters, last changed 2015-08-13 17:32 by mjpieters. This issue is now closed.

Messages (3)
msg248501 - (view) Author: Martijn Pieters (mjpieters) * Date: 2015-08-13 07:35
The documentation states that `side_effect` can be set to an [iterable](https://docs.python.org/3/glossary.html#term-iterable):

> If you pass in an iterable, it is used to retrieve an iterator which must yield a value on every call. This value can either be an exception instance to be raised, or a value to be returned from the call to the mock (`DEFAULT` handling is identical to the function case).

but the [actual handling of the side effect](https://github.com/testing-cabal/mock/blob/27a20329b25c8de200a8964ed5dd7762322e91f6/mock/mock.py#L1112-L1123) expects it to be an [*iterator*](https://docs.python.org/3/glossary.html#term-iterator):

if not _callable(effect):
    result = next(effect)

This excludes using a list or tuple object to produce the side effect sequence.

Can the documentation be updated to state an *iterator* is required (so an object that defines __next__ and who's __iter__ method returns self), or can the CallableMixin constructor be updated to call iter() on the side_effect argument if it is not an exception or a callable? You could even re-use the [_MockIter() class](https://hg.python.org/cpython/file/256d2f01e975/Lib/unittest/mock.py#l348) already used for the [NonCallableMock.side_effect property](https://hg.python.org/cpython/file/256d2f01e975/Lib/unittest/mock.py#l509).
msg248515 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-13 12:03
The documentation is accurate.  The object being manipulated by the code clause you site is not the original object passed in to the side_effect argument.
msg248530 - (view) Author: Martijn Pieters (mjpieters) * Date: 2015-08-13 17:32
Bugger, that's the last time I take someone's word for it and not test properly. Indeed, I missed the inheritance of NonCallableMock, so the property is inherited from there.

Mea Culpa!
History
Date User Action Args
2015-08-13 17:32:07mjpieterssetmessages: + msg248530
2015-08-13 12:03:03r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg248515

resolution: not a bug
stage: resolved
2015-08-13 07:35:53mjpieterscreate