classification
Title: mock._Any and mock._Call implement __eq__ but not __hash__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Roy Williams, nedbat, ztane
Priority: normal Keywords:

Created on 2016-09-23 17:59 by Roy Williams, last changed 2016-09-25 23:04 by berker.peksag. This issue is now closed.

Messages (6)
msg277290 - (view) Author: Roy Williams (Roy Williams) * Date: 2016-09-23 17:59
I am investigating a migration to Python 3, and to facilitate this we are using the -3 flag as decribed here: https://docs.python.org/3/howto/pyporting.html#prevent-compatibility-regressions . When using this flag I encountered
some issues inside of mock itself.

Python 3 now requires you to implement __hash__ if you implement __eq__. See https://docs.python.org/3.6/reference/datamodel.html#object.%5F%5Fhash%5F%5F .

{mock.ANY}  # Fine in Python 2, Throws in Python 3

I've created a PR that explicitly sets the __hash__ method on these objects as None to ensure the behavior is consistent in Python 3 as well as Python 2.  The folks over at testing-in-python suggested I submit a bug here first and then the fix can be backported to testing-cabal/mock

https://github.com/testing-cabal/mock/pull/378
msg277365 - (view) Author: Antti Haapala (ztane) * Date: 2016-09-25 11:24
So you mean that unittest.mock should explicitly set `__hash__ = None`; but this is already what the unittest.mock does, in Python 3; there is no need to set `__hash__ = None`; it is the backport that needs to do this in order to stay compatible. There is no mock.py in 2.7.
msg277382 - (view) Author: Roy Williams (Roy Williams) * Date: 2016-09-25 16:06
Hey Antti

My understanding is the version available on PyPi is now a strict backport
of what's in Python 3, so they suggested filling the bug in bugs.python.org
so it can be backported.

http://lists.idyll.org/pipermail/testing-in-python/2016-September/006864.html

Thanks,
Roy

On Sep 25, 2016 4:24 AM, "Antti Haapala" <report@bugs.python.org> wrote:

>
> Antti Haapala added the comment:
>
> So you mean that unittest.mock should explicitly set `__hash__ = None`;
> but this is already what the unittest.mock does, in Python 3; there is no
> need to set `__hash__ = None`; it is the backport that needs to do this in
> order to stay compatible. There is no mock.py in 2.7.
>
> ----------
> nosy: +ztane
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue28260>
> _______________________________________
>
msg277387 - (view) Author: Antti Haapala (ztane) * Date: 2016-09-25 18:01
Well, as far as I understand, this is a bug in the backport only. The Python 2.6+ backport does not behave identically to Python 3 code in Python 2, unless `__hash__ = None` is added. unittest.mock in Python 3 is not affected in any way.
msg277388 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2016-09-25 18:16
Roy, the code on GitHub isn't a literal copy of the Python 3 code, since it uses six, "from __future__ import", conditional imports of builtins, and so on.

This can be fixed in GitHub.
msg277395 - (view) Author: Roy Williams (Roy Williams) * Date: 2016-09-25 18:41
OK, let's close this issue and I'll ping the TIP thread/this issue again.

On Sun, Sep 25, 2016, 11:16 AM Ned Batchelder <report@bugs.python.org>
wrote:

>
> Ned Batchelder added the comment:
>
> Roy, the code on GitHub isn't a literal copy of the Python 3 code, since
> it uses six, "from __future__ import", conditional imports of builtins, and
> so on.
>
> This can be fixed in GitHub.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue28260>
> _______________________________________
>
History
Date User Action Args
2016-09-25 23:04:28berker.peksagsetstatus: open -> closed
resolution: not a bug
stage: resolved
2016-09-25 18:41:14Roy Williamssetmessages: + msg277395
2016-09-25 18:16:35nedbatsetmessages: + msg277388
2016-09-25 18:01:30ztanesetmessages: + msg277387
2016-09-25 16:06:40Roy Williamssetmessages: + msg277382
2016-09-25 11:24:40ztanesetnosy: + ztane
messages: + msg277365
2016-09-24 11:16:53nedbatsetnosy: + nedbat
2016-09-23 20:02:42Roy Williamssettype: behavior
2016-09-23 17:59:53Roy Williamscreate