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.
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:07 | admin | set | github: 66514 |
| 2014年09月01日 04:57:01 | r.david.murray | set | messages: + msg226205 |
| 2014年09月01日 01:25:03 | r.david.murray | set | status: open -> closed nosy: + r.david.murray messages: + msg226198 resolution: not a bug stage: resolved |
| 2014年08月31日 21:55:54 | TonyFlury | create | |