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?
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!
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.
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.