[New-bugs-announce] [issue22541] Support both side_effect and return_value in a more human way

Dima Tisnek report at bugs.python.org
Thu Oct 2 15:29:56 CEST 2014


New submission from Dima Tisnek:

Original use case:

I want to mock something that on invocation does 2 things:
* updates database
* returns custom, unrelated value

The problem:
side_effect is a catch-all setting that is used for:
* true side-effects
* return values
* exceptions

Moreover, side_effect takes precedence over return_value, unless the earlier returns mock.DEFAULT. Very quirky.


Proposed change option 1:
Side-effect is not used as return value; explicit return value may be used for that purpose.
(probably changes a lot of tests)

Proposed change option 2:
return_value, being more specific and explicit, takes precedence over side_effect; that is side_effect is still executed but it's rv is lost if overwritten by return_value is set.
(this is unlikely to change existing tests, as it is currently very odd to use both side_effect and return_value at the same time.)


Current workaround 1:
mock.Mock(side_effect=lambda *args: [db.update(), mock.DEFAULT][-1],
          return_value=42)

Current workaround 2:
mock.Mock(side_effect=lambda *args: [db.update(), 42][-1])

----------
components: Library (Lib)
messages: 228224
nosy: Dima.Tisnek
priority: normal
severity: normal
status: open
title: Support both side_effect and return_value in a more human way
type: enhancement
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5

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


More information about the New-bugs-announce mailing list