Issue 20968: mock.MagicMock does not mock truediv (original) (raw)

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

Files
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 "", line 1, in 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 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 ____, __i__, __r__). 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 http://hg.python.org/cpython/rev/445ef3b58109
msg216101 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-04-14 15:44
Thanks!
History
Date User Action Args
2022-04-11 14:58:00 admin set github: 65167
2014-04-14 15:44:42 michael.foord set status: open -> closedmessages: + assignee: michael.foordresolution: fixedstage: resolved
2014-04-14 15:27:04 python-dev set nosy: + python-devmessages: +
2014-04-02 05:31:55 rhettinger set nosy: + rhettingermessages: +
2014-03-31 11:26:20 Johannes.Baiter set messages: +
2014-03-19 13:02:57 Johannes.Baiter set files: + mock_truediv_with_tests.diff
2014-03-19 12:07:03 michael.foord set messages: +
2014-03-19 11:59:13 Johannes.Baiter set messages: +
2014-03-19 11:06:16 michael.foord set messages: +
2014-03-19 00:09:56 ned.deily set nosy: + michael.foord
2014-03-18 14:37:23 Johannes.Baiter set files: + mock_truediv_numerics.diffmessages: +
2014-03-18 14:27:01 Johannes.Baiter set files: + mock_truediv.diffkeywords: + patchmessages: +
2014-03-18 14:13:34 Johannes.Baiter create