[New-bugs-announce] [issue24856] Mock.side_effect as iterable or iterator

Martijn Pieters report at bugs.python.org
Thu Aug 13 09:35:53 CEST 2015


New submission from Martijn Pieters:

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).

----------
components: Library (Lib)
messages: 248501
nosy: mjpieters
priority: normal
severity: normal
status: open
title: Mock.side_effect as iterable or iterator
versions: Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24856>
_______________________________________


More information about the New-bugs-announce mailing list