[New-bugs-announce] [issue34716] MagicMock.__divmod__ should return a pair

Serhiy Storchaka report at bugs.python.org
Mon Sep 17 15:45:58 EDT 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

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?

----------
components: Library (Lib)
messages: 325571
nosy: michael.foord, serhiy.storchaka
priority: normal
severity: normal
status: open
title: MagicMock.__divmod__ should return a pair
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34716>
_______________________________________


More information about the New-bugs-announce mailing list