[issue22318] unitest documentation on assertItemsEqual misleading
New submission from Tony Flury: The notes on assertItemsEqual do not make it clear that the comparison works by using their hash value, and not their __eq__ implementation - i.e. it does an 'is' not an '==' between objects in the sequence. If the sequences being compared contain user created objects which don't implement their own specific __hash__ method (and therefore inherit their __hash__ from the base object - based on the object id), then the assertion will ALWAYS be false, regardless of their __eq__ value. This only became clear when trying to use unitest, getting strange results, and I eventually read the code. ---------- assignee: docs@python components: Documentation messages: 226193 nosy: TonyFlury, docs@python priority: normal severity: normal status: open title: unitest documentation on assertItemsEqual misleading type: behavior versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22318> _______________________________________
R. David Murray added the comment: I'm not sure what you saw when "reading the code". I don't see any 'a is b' in there (only 'elem is NULL', which is correct). This has nothing to do with unittest, and everything to do with how == is defined/implemented in python. The invariant is that if two objects are equal, their hashes *must* be equal, and the hash check is used as a fastpath to skip items that are not equal without doing a full comparison. If you implement __eq__, you must implement __hash__ (or set it to None, thus marking the objects as not hashable). Not doing so is an error in Python3 (and probably also in python2 if you are using new style classes, but I didn't check). So, it is your objects that are buggy, I'm afraid, not unittest. ---------- nosy: +r.david.murray resolution: -> not a bug stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22318> _______________________________________
R. David Murray added the comment: Is is an object identity test, by the way, it has nothing to do with __hash__. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue22318> _______________________________________
participants (2)
-
R. David Murray -
Tony Flury