Title: Python 2.7's `-3` flag warns about __eq__ being implemented without __hash__ even if __hash__ is never accessed.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Roy Williams, christian.heimes, nedbat, ztane
Priority: normal Keywords:

Created on 2016-09-23 23:23 by Roy Williams, last changed 2016-09-24 12:58 by christian.heimes. This issue is now closed.

Messages (3)
msg277305 - (view) Author: Roy Williams (Roy Williams) * Date: 2016-09-23 23:23
I'm finding the -3 flag to be super useful at identifying problems with code when porting to Python 3.  One of the most common failures, however, is "DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x".  While implementing __eq__ without implementing __hash__ is very much an anti-pattern, this warning would be much less noisy if it could be thrown at time of access (like the __getslice__ warning) instead of time of declaration. 

See Also:
msg277309 - (view) Author: Antti Haapala (ztane) * Date: 2016-09-24 07:01
I am very negative to this idea. Correct code in Python **2** would either set `__hash__ = None` or redefine `__hash__` in *any* class that defines `__eq__`. That it just wasn't used like that is no excuse.

This warning is even more important if even Ned Batchelder could have a bug like that in his code.

It will break as soon as someone "realizes" that "hey I can use a set to remove duplicates in my container".
msg277317 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-24 12:58
Antti is correct. Please add __hash__ = None to your class to silence the warning.
Date User Action Args
2016-09-24 12:58:40christian.heimessetstatus: open -> closed

nosy: + christian.heimes
messages: + msg277317

resolution: not a bug
stage: resolved
2016-09-24 11:16:31nedbatsetnosy: + nedbat
2016-09-24 07:01:09ztanesetnosy: + ztane
messages: + msg277309
2016-09-23 23:23:01Roy Williamscreate