Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue20968

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:mock.MagicMock does not mock __truediv__
Type:behaviorStage:resolved
Components:Library (Lib)Versions:Python 3.4
process
Status:closedResolution:fixed
Dependencies:Superseder:
Assigned To: michael.foordNosy List: Johannes.Baiter, michael.foord, python-dev, rhettinger
Priority:normalKeywords:patch

Created on2014-03-18 14:13 byJohannes.Baiter, last changed2022-04-11 14:58 byadmin. This issue is nowclosed.

Files
File nameUploadedDescriptionEdit
mock_truediv.diffJohannes.Baiter,2014-03-18 14:27Make unittest.mock.MagicMock mock __truediv__ and __rtruediv__review
mock_truediv_numerics.diffJohannes.Baiter,2014-03-18 14:37Add 'truediv' to global 'numerics' instead of 'magic_methods'review
mock_truediv_with_tests.diffJohannes.Baiter,2014-03-19 13:02Add testsreview
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 / 2Traceback (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 changeset445ef3b58109 by Michael Foord in branch '3.4':Issue 20968. unittest.mock.MagicMock now supports divisionhttp://hg.python.org/cpython/rev/445ef3b58109
msg216101 -(view)Author: Michael Foord (michael.foord)*(Python committer)Date: 2014-04-14 15:44
Thanks!
History
DateUserActionArgs
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
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp