Issue 34716: MagicMock.divmod should return a pair (original) (raw)

Created on 2018-09-17 19:45 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 17291 open jacksonriley,2019-11-20 14:47
Messages (5)
msg325571 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-09-17 19:45
It is documented, that divmod() returns a pair. It is usually used with tuple unpacking: x, y = divmod(a, b) But this doesn't work when one of arguments is a MagicMock. >>> from unittest.mock import * >>> x, y = divmod(MagicMock(), 2) Traceback (most recent call last): File "", line 1, in ValueError: not enough values to unpack (expected 2, got 0) I expect that tuple unpacking will work with the result of MagicMock.__divmod__(). There possible following options: 1. Return some constant value, e.g. (1, 0). 2. Return a pair of new MagicMock instances. 3. Define __divmod__ in terms of __floordiv__ and __mod__. This will automatically return a pair of MagicMock instances by default, but setting return values for __floordiv__ and __mod__ will affect the result of __divmod__. What is more preferable?
msg356469 - (view) Author: Jackson Riley (jacksonriley) * Date: 2019-11-12 15:12
Hi Serhiy, Option 3 sounds most sensible to me. I'd be happy to pick up this issue, please do let me know.
msg356525 - (view) Author: Jackson Riley (jacksonriley) * Date: 2019-11-13 09:30
On second thoughts, perhaps option 2 is best (more in keeping with the usual behaviour of MagicMock). Alternatively, could I propose a fourth option: 4. Change the behaviour of MagicMock more generally such that trying to unpack a MagicMock instance into two variables, for example, would assign a new MagicMock instance to each. This would fix this issue and also seems like a sensible thing for MagicMock in general. I may be missing something/this may not be easy to set-up, I don't know!
msg356544 - (view) Author: Vedran Čačić (veky) * Date: 2019-11-13 17:59
Yes, this is impossible using only "universal Python" (independent of implementation). Though, of course, it's possible in CPython if you analyze the source code (or AST) and count the targets on the left side.
msg357045 - (view) Author: Jackson Riley (jacksonriley) * Date: 2019-11-20 10:04
Ah thank you Vedran, that makes sense. In that case, I think I'll make a start on implementing Serhiy's second suggestion - returning a pair of MagicMock instances when MagicMock.__divmod__ is called.
History
Date User Action Args
2022-04-11 14:59:06 admin set github: 78897
2020-10-19 20:49:44 zach.ware set versions: + Python 3.10, - Python 3.6, Python 3.7, Python 3.8
2019-11-26 05:16:40 xtreak set nosy: + xtreak
2019-11-20 14:47:45 jacksonriley set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest16784>
2019-11-20 10:04:41 jacksonriley set messages: +
2019-11-13 17:59:08 veky set nosy: + vekymessages: +
2019-11-13 09:30:19 jacksonriley set messages: +
2019-11-12 15:12:09 jacksonriley set nosy: + jacksonrileymessages: +
2018-09-17 19:55:24 serhiy.storchaka link issue34676 dependencies
2018-09-17 19:49:37 p-ganssle set nosy: + p-ganssle
2018-09-17 19:45:58 serhiy.storchaka create