Title: Mock.side_effect as iterable or iterator
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.4, Python 3.5
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](

> 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]( expects it to be an [*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]( already used for the [NonCallableMock.side_effect property](
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!
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