Issue20968
Created on 2014-03-18 14:13 by Johannes.Baiter, last changed 2014-04-14 15:44 by michael.foord. 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 "<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) * | 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) * | 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! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2014-04-14 15:44:42 | michael.foord | set | status: open -> closed messages: + msg216101 assignee: michael.foord |
| 2014-04-14 15:27:04 | python-dev | set | nosy:
+ python-dev messages: + msg216094 |
| 2014-04-02 05:31:55 | rhettinger | set | nosy:
+ rhettinger messages: + msg215357 |
| 2014-03-31 11:26:20 | Johannes.Baiter | set | messages: + msg215228 |
| 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: + msg214084 |
| 2014-03-19 11:59:13 | Johannes.Baiter | set | messages: + msg214083 |
| 2014-03-19 11:06:16 | michael.foord | set | messages: + msg214077 |
| 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.diff messages: + msg213973 |
| 2014-03-18 14:27:01 | Johannes.Baiter | set | files:
+ mock_truediv.diff keywords: + patch messages: + msg213968 |
| 2014-03-18 14:13:34 | Johannes.Baiter | create | |