Author Dima.Tisnek
Recipients Dima.Tisnek
Date 2014-10-02.13:29:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1412256596.08.0.0171916837532.issue22541@psf.upfronthosting.co.za>
In-reply-to
Content
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])
History
Date User Action Args
2014-10-02 13:29:56Dima.Tisneksetrecipients: + Dima.Tisnek
2014-10-02 13:29:56Dima.Tisneksetmessageid: <1412256596.08.0.0171916837532.issue22541@psf.upfronthosting.co.za>
2014-10-02 13:29:56Dima.Tisneklinkissue22541 messages
2014-10-02 13:29:55Dima.Tisnekcreate