Issue 22452: addTypeEqualityFunc is not used in assertListEqual (original) (raw)
Created on 2014-09-21 11:12 by simonzack, last changed 2022-04-11 14:58 by admin.
Messages (6)
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.
Author: Antoine Pitrou (pitrou) *
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.
Author: Ezio Melotti (ezio.melotti) *
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.
Author: Robert Collins (rbcollins) *
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.
Author: Robert Collins (rbcollins) *
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..."
Author: Irit Katriel (iritkatriel) *
Date: 2021-08-07 19:03
I've closed as a duplicate of this.
History
Date
User
Action
Args
2022-04-11 14:58:08
admin
set
github: 66642
2021-08-07 19:03:47
iritkatriel
set
nosy: + iritkatriel
messages: +
versions: + Python 3.11, - Python 3.5
2021-08-04 17:40:20
iritkatriel
link
2015-08-18 14:00:24
anton.barkovsky
set
nosy: + anton.barkovsky
2014-11-07 10:30:35
rbcollins
set
messages: +
2014-11-07 09:58:16
rbcollins
set
messages: +
2014-11-01 22:09:27
ezio.melotti
set
nosy: + rbcollins
2014-09-21 18:29:23
ezio.melotti
set
nosy: + serhiy.storchaka
messages: +
2014-09-21 16:36:19
r.david.murray
set
nosy: + r.david.murray
2014-09-21 11:16:05
pitrou
set
versions: + Python 3.5
nosy: + ezio.melotti, michael.foord, pitrou
messages: +
stage: needs patch
2014-09-21 11:12:16
simonzack
set
type: enhancement
2014-09-21 11:12:08
simonzack
create