classification
Title: unittest.assertEqual() on un-equal types inheriting from collections.Mapping
Type: enhancement Stage:
Components: Tests Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Nathan Herring, ezio.melotti, michael.foord, rbcollins, rhettinger
Priority: normal Keywords:

Created on 2015-11-19 14:59 by Nathan Herring, last changed 2015-11-23 11:40 by michael.foord. 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) * (Python committer) 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
2015-11-23 11:40:48michael.foordsetstatus: open -> closed
resolution: rejected
messages: + msg255149
2015-11-20 10:52:24serhiy.storchakasetnosy: + rbcollins, ezio.melotti, michael.foord
2015-11-20 09:07:22rhettingersetversions: + Python 3.6, - Python 2.7
2015-11-20 09:07:10rhettingersetassignee: rhettinger ->
2015-11-20 06:16:09rhettingersetassignee: rhettinger

nosy: + rhettinger
2015-11-19 15:12:01Nathan Herringsetmessages: + msg254904
2015-11-19 14:59:24Nathan Herringcreate