[TIP] MagicMocks and inequalities

Dan Wandschneider daniel.wandschneider at schrodinger.com
Mon Jan 28 10:48:32 PST 2013


Michael-
Thanks for your response.  I'm using mock.py version 1.0.  I'll try to
upgrade to mock 1.0.1, and if I still see this behavior, I'll definitely
submit this as a bug.

Note that your test didn't really demonstrate whether __lt__ was
"implemented", since it would be called even if the return value were
NotImplemented.  In my copy of mock.py (version 1.0), I see this:
_return_values = {
    '__lt__': NotImplemented,
    '__gt__': NotImplemented,
    '__le__': NotImplemented,
    '__ge__': NotImplemented,
   etc.

And this behavior:

>>> from mock import MagicMock
>>> m = MagicMock()
>>> m.__lt__
<MagicMock name='mock.__lt__' id='47908705590288'>
>>> m.__lt__()
NotImplemented
>>> m < 3
False
>>> m.__lt__.call_count
2

Thanks again for this great tool,
 Dan W.







On Mon, Jan 28, 2013 at 3:28 AM, Michael Foord <fuzzyman at voidspace.org.uk>wrote:

>
> 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
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130128/8ced1ac6/attachment.htm>


More information about the testing-in-python mailing list