I was able to find a suitable solution for a long time, thanks to the author of this gist https://gist.github.com/Back2Basics/0e6456de0395fd200a06
import datetime, time, functools, operator, allure default_fudge = datetime.timedelta(seconds=0, microseconds=0, days=0) def deep_eq(_v1, _v2, datetime_fudge=default_fudge, _assert=True): """ Tests for deep equality between two python data structures recursing into sub-structures if necessary. Works with all python types including iterators and generators. This function was dreampt up to test API responses but could be used for anything. Be careful. With deeply nested structures you may blow the stack. Options: datetime_fudge => this is a datetime.timedelta object which, when comparing dates, will accept values that differ by the number of seconds specified _assert => passing yes for this will raise an assertion error when values do not match, instead of returning false (very useful in combination with pdb) """ _deep_eq = functools.partial(deep_eq, datetime_fudge=datetime_fudge, _assert=_assert) def _check_assert(R, a, b, reason=''): if _assert and not R: assert 0, "an assertion has failed in deep_eq (%s) %s != %s" % ( reason, str(a), str(b)) return R def _deep_dict_eq(d1, d2): k1, k2 = (sorted(d1.keys()), sorted(d2.keys())) if k1 != k2: # keys should be exactly equal return _check_assert(False, k1, k2, "keys") return _check_assert(operator.eq(sum(_deep_eq(d1[k], d2[k]) for k in k1), len(k1)), d1, d2, "dictionaries") def _deep_iter_eq(l1, l2): if len(l1) != len(l2): return _check_assert(False, l1, l2, "lengths") return _check_assert(operator.eq(sum(_deep_eq(v1, v2) for v1, v2 in zip(l1, l2)), len(l1)), l1, l2, "iterables") def op(a, b): _op = operator.eq if type(a) == datetime.datetime and type(b) == datetime.datetime: s = datetime_fudge.seconds t1, t2 = (time.mktime(a.timetuple()), time.mktime(b.timetuple())) l = t1 - t2 l = -l if l > 0 else l return _check_assert((-s if s > 0 else s) <= l, a, b, "dates") return _check_assert(_op(a, b), a, b, "values") c1, c2 = (_v1, _v2) # guard against strings because they are iterable and their # elements yield iterables infinitely. # INCEPTION if isinstance(_v1, str): pass else: if isinstance(_v1, dict): op = _deep_dict_eq else: try: c1, c2 = (list(iter(_v1)), list(iter(_v2))) except TypeError: c1, c2 = _v1, _v2 else: op = _deep_iter_eq return op(c1, c2)
As a result, with _assert = True enabled, the problem is also displayed:
R = False, a = '1.17', b = '1.19', reason = 'values' def _check_assert(R, a, b, reason=''): if _assert and not R: assert 0, "an assertion has failed in deep_eq (%s) %s != %s" % ( > reason, str(a), str(b)) E AssertionError: an assertion has failed in deep_eq (values) 1.17 != 1.19
minimaldifference between the values, so in this case there is no way) - andreymal