Message228224
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]) |
|
Date |
User |
Action |
Args |
2014-10-02 13:29:56 | Dima.Tisnek | set | recipients:
+ Dima.Tisnek |
2014-10-02 13:29:56 | Dima.Tisnek | set | messageid: <1412256596.08.0.0171916837532.issue22541@psf.upfronthosting.co.za> |
2014-10-02 13:29:56 | Dima.Tisnek | link | issue22541 messages |
2014-10-02 13:29:55 | Dima.Tisnek | create | |
|