diff --git a/dictdiffer/utils.py b/dictdiffer/utils.py index 6816a03..3b1c3eb 100644 --- a/dictdiffer/utils.py +++ b/dictdiffer/utils.py @@ -264,10 +264,19 @@ def are_different(first, second, tolerance): return False first_is_nan, second_is_nan = bool(first != first), bool(second != second) + first_is_inf, second_is_inf = first == float('inf'), second == float('inf') + first_is_ninf = first == float('-inf') + second_is_ninf = second == float('-inf') if first_is_nan or second_is_nan: # two 'NaN' values are not different (see issue #114) return not (first_is_nan and second_is_nan) + if first_is_inf or second_is_inf: + # two 'inf' values are not different + return not (first_is_inf and second_is_inf) + if first_is_ninf or second_is_ninf: + # two '-inf' values are not different + return not (first_is_ninf and second_is_ninf) elif isinstance(first, num_types) and isinstance(second, num_types): # two numerical values are compared with tolerance return abs(first-second) > tolerance * max(abs(first), abs(second)) diff --git a/tests/test_dictdiffer.py b/tests/test_dictdiffer.py index 205f546..b9cea33 100644 --- a/tests/test_dictdiffer.py +++ b/tests/test_dictdiffer.py @@ -277,6 +277,35 @@ def test_numpy_nan(self): result = list(diff(first, second)) assert result == [] + def test_inf(self): + first = {'a': [1.0, 2.0, 3.0, 4.0]} + second = {'a': [1.0, 2.0, 3.0, float('inf')]} + diffed = next(diff(first, second)) + assert ('change', ['a', 3], (4, float('inf'))) == diffed + + first = {'a': [1.0, 2.0, 3.0, 4.0]} + second = {'a': [1.0, 2.0, 3.0, float('-inf')]} + diffed = next(diff(first, second)) + assert ('change', ['a', 3], (4, float('-inf'))) == diffed + + first = {'a': [1.0, 2.0, 3.0, float('inf')]} + second = {'a': [1.0, 2.0, 3.0, float('inf')]} + result = list(diff(first, second)) + assert result == [] + + first = {'a': [1.0, 2.0, 3.0, float('inf')]} + second = {'a': [1.0, 2.0, 3.0, float('-inf')]} + diffed = next(diff(first, second)) + assert ('change', ['a', 3], (float('inf'), float('-inf'))) == diffed + + @unittest.skipIf(not HAS_NUMPY, 'NumPy is not installed') + def test_numpy_inf(self): + import numpy as np + first = {'a': [1.0, 2.0, 3.0, 4.0]} + second = {'a': [1.0, 2.0, 3.0, np.float64('inf')]} + diffed = next(diff(first, second)) + assert ('change', ['a', 3], (4, np.float64('inf'))) == diffed + def test_unicode_keys(self): first = {u'привет': 1} second = {'hello': 1}