From e1d627db7ecb5bff96f9ca04ab4c07c643a8cf31 Mon Sep 17 00:00:00 2001 From: Eric Bonfadini <408262+eric-bonfadini@users.noreply.github.com> Date: Wed, 10 Feb 2021 00:37:32 +0100 Subject: [PATCH 1/2] Fix are_different check for infs --- dictdiffer/utils.py | 8 ++++++++ tests/test_dictdiffer.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/dictdiffer/utils.py b/dictdiffer/utils.py index 6816a03..dad7aec 100644 --- a/dictdiffer/utils.py +++ b/dictdiffer/utils.py @@ -264,10 +264,18 @@ 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, second_is_ninf = first == float('-inf'), 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} From 6b897b778345a53bdcaf72d4cdaf034cb4162066 Mon Sep 17 00:00:00 2001 From: Eric Bonfadini <408262+eric-bonfadini@users.noreply.github.com> Date: Wed, 10 Feb 2021 00:54:10 +0100 Subject: [PATCH 2/2] Fixed pycodestyle issue --- dictdiffer/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dictdiffer/utils.py b/dictdiffer/utils.py index dad7aec..3b1c3eb 100644 --- a/dictdiffer/utils.py +++ b/dictdiffer/utils.py @@ -265,7 +265,8 @@ def are_different(first, second, tolerance): 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, second_is_ninf = 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)