[TIP] MagicMocks and inequalities

Michael Foord fuzzyman at voidspace.org.uk
Mon Jan 28 03:28:04 PST 2013


On 18 Jan 2013, at 19:52, Dan Wandschneider <daniel.wandschneider at schrodinger.com> wrote:

> Under Python 2.7.3 and Mock 1.0.0, we've noticed some strange behavior.    On Linux, 0 < MagicMock(), and on Windows 0 > MagicMock().  Has anyone else noticed similar behavior?  Should this be considered a bug?

That would be a bug, yes. One I'm very surprised about. I don't think I even have a working Windows VM to try this on though - so I'd be very interested to see if anyone else can verify it.

> 
> Note that MagicMock does not implement the inequality comparison operators, which means that in inequality comparisons it delegates the comparison to the other object.  Thus, the expression "MagicMock() > 0" is ultimately evaluated as "0 < MagicMock()."  See Lennert Regebro's cogent post about total ordering.

MagicMock *does* implement all the equality and comparison methods. The one used in a "less than" test is __lt__ :

>>> from mock import MagicMock
>>> m = MagicMock()
>>> m.__lt__
<MagicMock name='mock.__lt__' id='4300924432'>
>>> m < 0
False
>>> m > 0
True
>>> m.__lt__.call_count
1

All the best,

Michael Foord

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


--
http://www.voidspace.org.uk/


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing 
http://www.sqlite.org/different.html








More information about the testing-in-python mailing list