Issue 25669: unittest.assertEqual() on un-equal types inheriting from collections.Mapping (original) (raw)
Issue25669
Created on 2015-11-19 14:59 by Nathan Herring, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Messages (3) | ||
---|---|---|
msg254902 - (view) | Author: Nathan Herring (Nathan Herring) | Date: 2015-11-19 14:59 |
We have some code that generates objects that inherit from Mapping that are not nearly as straightforward to instantiate in a test. It's much easier to have something like the follows: foo = … # some collections.Mapping subtype self.assertEqual({'key': 'value'}, foo) unittest/case.py's _baseAssertEqual will print out both sides and let me do visual inspection, but I'd prefer it if it knew both sides supported Mapping and in the != case, performed an analog of assertDictEqual so I could just get the keys/values that were mismatched, especially in the cases of large dictionaries. | ||
msg254904 - (view) | Author: Nathan Herring (Nathan Herring) | Date: 2015-11-19 15:12 |
FWIW, it's also pretty easy to write self.assertEqual({'key': 'value'}, dict(foo)) and get the right behavior, but it'd be nice if it was done automagically by type checking. | ||
msg255149 - (view) | Author: Michael Foord (michael.foord) * ![]() |
Date: 2015-11-23 11:40 |
assertEqual *does* do type checking and it's strict that it will only resort to the "type specific" assert checks if both types are of the same type. In the general case it's impossible to know whether comparing a subclass with the type specific check is the right thing to do - so unittest doesn't guess. As you have a simple workaround ( dict(foo) ) I'm closing this. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:24 | admin | set | github: 69855 |
2015-11-23 11:40:48 | michael.foord | set | status: open -> closedresolution: rejectedmessages: + |
2015-11-20 10:52:24 | serhiy.storchaka | set | nosy: + rbcollins, ezio.melotti, michael.foord |
2015-11-20 09:07:22 | rhettinger | set | versions: + Python 3.6, - Python 2.7 |
2015-11-20 09:07:10 | rhettinger | set | assignee: rhettinger -> |
2015-11-20 06:16:09 | rhettinger | set | assignee: rhettingernosy: + rhettinger |
2015-11-19 15:12:01 | Nathan Herring | set | messages: + |
2015-11-19 14:59:24 | Nathan Herring | create |