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: addTypeEqualityFunc is not used in assertListEqual
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: anton.barkovsky, ezio.melotti, iritkatriel, michael.foord, pitrou, r.david.murray, rbcollins, serhiy.storchaka, simonzack
Priority: normal Keywords:

Created on 2014-09-21 11:12 by simonzack, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg227210 - (view) Author: Simon Zack (simonzack) Date: 2014-09-21 11:12
Functions added by addTypeEqualityFunc is not used for comparing list elements in assertListEqual, and only used in assertEqual.

It would be nice to have assertListEqual use functions added by addTypeEqualityFunc for comparisons of list elements. I think this provides more flexibility, and we get nicely formatted error messages for nested list compares for free.
msg227212 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-09-21 11:16
That sounds reasonable to me. Do you want to provide a patch?
See https://docs.python.org/devguide/ for guidelines.
msg227222 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-09-21 18:29
Currently assertListEqual calls assertSequenceEqual, and assertSequenceEqual doesn't use any function to compare list elements -- it just does "if item1 != item2:" (https://hg.python.org/cpython/file/default/Lib/unittest/case.py).  Checking the types of the two items and compare them recursively could be done, but it's not as simple as it sounds, since using e.g. assertSequenceEqual will raise an error message that will need to be caught and integrated with the error message that it's already being constructed, possibly resulting in a long and unreadable message.

Since this is a somewhat specific situation, it might be better if you just defined your own assert function for nested lists.  In addition to nested lists you might have a dictionary that contains lists, or a set of tuples or any other combinations of arbitrarily nested containers, and having a generic way to handle them all will require quite a lot of work.

One thing that could be done is to add more attributes to the exception raised by assertSequenceEqual (and others), so that you could do something like:
try:
    self.assertEqual(nested_list1, nested_list2)
except AssertionError as exc:
    index = exc.first_differing_index
    self.assertEqual(nested_list1[index], nested_list2[index], msg=str(exc))

Not sure how that will look light though, and it's still not a generic solution, but if you know what are you dealing with, it might be helpful.
msg230776 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2014-11-07 09:58
https://code.google.com/p/unittest-ext/issues/detail?id=11

I think that the hamcrest inspired matchers stuff may help make this a reality too. OTOH if we had a clean patch now for the existing asserts that would be fine too.
msg230785 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2014-11-07 10:30
See also https://code.google.com/p/unittest-ext/issues/detail?id=27

"Sorry, wrong wording of the bug.

I tested this on IronPython 2.6.1 and 2.7.b1. I see the same result as you and I consider the following wrong or at least misleading:

- [1, Decimal("1"), Decimal("2.00")]
?  ^                          ---

+ [2, Decimal("1.00"), Decimal("2")]
?  ^            +++

I mean the +++ and --- under Decimal numbers.

On the other hand I understand that these are differences in string representation of those lists..."
msg399199 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-08-07 19:03
I've closed issue44819 as a duplicate of this.
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66642
2021-08-07 19:03:47iritkatrielsetnosy: + iritkatriel

messages: + msg399199
versions: + Python 3.11, - Python 3.5
2021-08-04 17:40:20iritkatriellinkissue44819 superseder
2015-08-18 14:00:24anton.barkovskysetnosy: + anton.barkovsky
2014-11-07 10:30:35rbcollinssetmessages: + msg230785
2014-11-07 09:58:16rbcollinssetmessages: + msg230776
2014-11-01 22:09:27ezio.melottisetnosy: + rbcollins
2014-09-21 18:29:23ezio.melottisetnosy: + serhiy.storchaka
messages: + msg227222
2014-09-21 16:36:19r.david.murraysetnosy: + r.david.murray
2014-09-21 11:16:05pitrousetversions: + Python 3.5
nosy: + ezio.melotti, michael.foord, pitrou

messages: + msg227212

stage: needs patch
2014-09-21 11:12:16simonzacksettype: enhancement
2014-09-21 11:12:08simonzackcreate