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.

Title: mock.MagicMock does not mock __truediv__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: Johannes.Baiter, michael.foord, python-dev, rhettinger
Priority: normal Keywords: patch

Created on 2014-03-18 14:13 by Johannes.Baiter, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
mock_truediv.diff Johannes.Baiter, 2014-03-18 14:27 Make unittest.mock.MagicMock mock __truediv__ and __rtruediv__ review
mock_truediv_numerics.diff Johannes.Baiter, 2014-03-18 14:37 Add 'truediv' to global 'numerics' instead of 'magic_methods' review
mock_truediv_with_tests.diff Johannes.Baiter, 2014-03-19 13:02 Add tests review
Messages (10)
msg213964 - (view) Author: Johannes Baiter (Johannes.Baiter) * Date: 2014-03-18 14:13
It seems that when creating a MagicMock the magic '__truediv__' method is not replaced with a mock:

>>> import mock
>>> foo = mock.MagicMock()
>>> foo / 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'MagicMock' and 'int'

The same thing works perfectly fine when using the third party module in Python 2.7, since the 2.x '__div__' seems to be mocked:

>>> import mock
>>> foo = mock.MagicMock()
>>> foo/2
<MagicMock name='mock.__div__()' id='139760595027088'>

To clarify the context, I am trying to mock a 'pathlib.Path' object in my unittest, which overloads the division operator, i.e. implements '__truediv__'.
msg213968 - (view) Author: Johannes Baiter (Johannes.Baiter) * Date: 2014-03-18 14:27
Attached is a patch that fixes the issue for me.
msg213973 - (view) Author: Johannes Baiter (Johannes.Baiter) * Date: 2014-03-18 14:37
I just noticed that I put the magic method names in the wrong place in the patch.
Attached is a fix that adds 'truediv' to the global 'numberics' variable, this way '__rtruediv__' and '__itruediv__' will be correctly mocked as well.
msg214077 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-03-19 11:06
A test would be nice please (good catch on the bug).
msg214083 - (view) Author: Johannes Baiter (Johannes.Baiter) * Date: 2014-03-19 11:59
From looking at 'test_numerics', only 'add' is currently tested.
Probably since the mechanism for all of the numeric magic methods is the same (i.e. create __<op>__, __i<op>__, __r<op>__).

Should I add a test for __truediv__ and its inplace and right variants nonetheless?
msg214084 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-03-19 12:07
Well, as this is a regression fix we definitley need a test. Ideally we would test all the operations - I didn't realise that only add was tested! However for this specific issue, just testing division is fine, and yes testing in place and right hand as well would be good.
msg215228 - (view) Author: Johannes Baiter (Johannes.Baiter) * Date: 2014-03-31 11:26
Sorry for commenting so late, I submitted a version of the patch with unit tests roughly two weeks ago, I just forgot to mention it in a comment. Hereby fixed :-)
msg215357 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-02 05:31
The patch looks clean and correct.  It passes the test suite.

I recommend going ahead and applying the patch.
msg216094 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-04-14 15:27
New changeset 445ef3b58109 by Michael Foord in branch '3.4':
Issue 20968. unittest.mock.MagicMock now supports division
msg216101 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-04-14 15:44
Date User Action Args
2022-04-11 14:58:00adminsetgithub: 65167
2014-04-14 15:44:42michael.foordsetstatus: open -> closed
messages: + msg216101

assignee: michael.foord
resolution: fixed
stage: resolved
2014-04-14 15:27:04python-devsetnosy: + python-dev
messages: + msg216094
2014-04-02 05:31:55rhettingersetnosy: + rhettinger
messages: + msg215357
2014-03-31 11:26:20Johannes.Baitersetmessages: + msg215228
2014-03-19 13:02:57Johannes.Baitersetfiles: + mock_truediv_with_tests.diff
2014-03-19 12:07:03michael.foordsetmessages: + msg214084
2014-03-19 11:59:13Johannes.Baitersetmessages: + msg214083
2014-03-19 11:06:16michael.foordsetmessages: + msg214077
2014-03-19 00:09:56ned.deilysetnosy: + michael.foord
2014-03-18 14:37:23Johannes.Baitersetfiles: + mock_truediv_numerics.diff

messages: + msg213973
2014-03-18 14:27:01Johannes.Baitersetfiles: + mock_truediv.diff
keywords: + patch
messages: + msg213968
2014-03-18 14:13:34Johannes.Baitercreate