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) *  |
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) *  |
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) *  |
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)  |
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) *  |
Date: 2014-04-14 15:44 |
Thanks! |
|
|