This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unitest documentation on assertItemsEqual misleading
Type: behavior Stage: resolved
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: TonyFlury, docs@python, r.david.murray
Priority: normal Keywords:

Created on 2014-08-31 21:55 by TonyFlury, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg226193 - (view) Author: Tony Flury (TonyFlury) Date: 2014-08-31 21:55
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.
msg226198 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-01 01:25
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.
msg226205 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-01 04:57
Is is an object identity test, by the way, it has nothing to do with __hash__.
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66514
2014-09-01 04:57:01r.david.murraysetmessages: + msg226205
2014-09-01 01:25:03r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg226198

resolution: not a bug
stage: resolved
2014-08-31 21:55:54TonyFlurycreate