-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mock.MagicMock does not mock __truediv__ #65167
Comments
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__'. |
Attached is a patch that fixes the issue for me. |
I just noticed that I put the magic method names in the wrong place in the patch. |
A test would be nice please (good catch on the bug). |
From looking at 'test_numerics', only 'add' is currently tested. Should I add a test for __truediv__ and its inplace and right variants nonetheless? |
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. |
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 :-) |
The patch looks clean and correct. It passes the test suite. I recommend going ahead and applying the patch. |
New changeset 445ef3b58109 by Michael Foord in branch '3.4': |
Thanks! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: