[New-bugs-announce] [issue20968] mock.MagicMock does not mock __truediv__

Johannes Baiter report at bugs.python.org
Tue Mar 18 15:13:34 CET 2014


New submission from Johannes Baiter:

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__'.

----------
components: Library (Lib)
messages: 213964
nosy: Johannes.Baiter
priority: normal
severity: normal
status: open
title: mock.MagicMock does not mock __truediv__
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20968>
_______________________________________


More information about the New-bugs-announce mailing list