This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: MagicMock.__divmod__ should return a pair
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jacksonriley, michael.foord, p-ganssle, serhiy.storchaka, veky, xtreak
Priority: normal Keywords: patch

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 "<stdin>", line 1, in <module>
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:06adminsetgithub: 78897
2020-10-19 20:49:44zach.waresetversions: + Python 3.10, - Python 3.6, Python 3.7, Python 3.8
2019-11-26 05:16:40xtreaksetnosy: + xtreak
2019-11-20 14:47:45jacksonrileysetkeywords: + patch
stage: patch review
pull_requests: + pull_request16784
2019-11-20 10:04:41jacksonrileysetmessages: + msg357045
2019-11-13 17:59:08vekysetnosy: + veky
messages: + msg356544
2019-11-13 09:30:19jacksonrileysetmessages: + msg356525
2019-11-12 15:12:09jacksonrileysetnosy: + jacksonriley
messages: + msg356469
2018-09-17 19:55:24serhiy.storchakalinkissue34676 dependencies
2018-09-17 19:49:37p-gansslesetnosy: + p-ganssle
2018-09-17 19:45:58serhiy.storchakacreate