[TIP] MagicMocks and inequalities
Michael Foord
fuzzyman at voidspace.org.uk
Mon Mar 18 17:38:36 PDT 2013
On 28 Jan 2013, at 11:16, Dan Wandschneider <daniel.wandschneider at schrodinger.com> wrote:
> Michael-
> I just downloaded mock version 1.0.1 and it still appears that __lt__ is not implemented. I also still see the platform dependent behavior of __lt__. I've submitted this as bug#196.
Note that MagicMock().__lt__ (and friends) returning NotImplemented is very deliberate. This platform dependent behaviour is because when comparisons return NotImplemented on *both sides* Python defaults to comparing the types. (Python 2 that is - Python 3 does the sane thing and raises a TypeError.)
The following thing returns different values on Windows and Linux/Mac:
mock.MagicMock < int
This is odd - and is a weird platform difference in Python itself. There's nothing that mock can do about this I'm afraid. If you *care* about the result of a comparison with a magic mock then you need to be setting it explicitly.
Sorry about that.
Also note that in MagicMock, __lt__ (etc) *are* implemented. They are deliberately returning NotImplemented. You can change this:
>>> from mock import MagicMock
>>> m = MagicMock()
>>> m.__lt__.return_value = True
>>> m < 0
True
All the best,
Michael Foord
> Thanks-
> Dan W.
>
>
>
> On Mon, Jan 28, 2013 at 10:48 AM, Dan Wandschneider <daniel.wandschneider at schrodinger.com> wrote:
> 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
>
>
>
>
>
>
>
--
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