Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,11 @@ def _diff_dict(
self._report_result(item_removed_key, change_level, local_tree=local_tree)

for key in t_keys_intersect: # key present in both dicts - need to compare values
if self._count_diff() is StopIteration:
return # pragma: no cover. This is already covered for addition.

key1 = t1_clean_to_keys[key] if t1_clean_to_keys else key
key2 = t2_clean_to_keys[key] if t2_clean_to_keys else key
if not self._is_ignored_nan_inequality(t1[key1], t2[key2]) and self._count_diff() is StopIteration:
return # pragma: no cover. This is already covered for addition.

item_id = id(t1[key1])
if parents_ids and item_id in parents_ids:
continue
Expand Down Expand Up @@ -1079,7 +1079,7 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
t2_from_index=t2_from_index, t2_to_index=t2_to_index,
match_key_spec=match_key_spec,
):
if self._count_diff() is StopIteration:
if not self._is_ignored_nan_inequality(x, y) and self._count_diff() is StopIteration:
return # pragma: no cover. This is already covered for addition.

reference_param1 = i
Expand Down Expand Up @@ -2122,6 +2122,15 @@ def _diff_types(self, level, local_tree=None):
level.report_type = 'type_changes'
self._report_result('type_changes', level, local_tree=local_tree)

def _is_ignored_nan_inequality(self, t1, t2):
"""Whether comparing t1 and t2 is a NaN-vs-NaN comparison that
ignore_nan_inequality forgives, i.e. the pair is not a diff."""
return bool(
self.ignore_nan_inequality
and isinstance(t1, (float, np_floating))
and str(t1) == str(t2) == 'nan'
)

def _count_diff(self):
if (self.max_diffs is not None and self._stats[DIFF_COUNT] > self.max_diffs):
if not self._stats[MAX_DIFF_LIMIT_REACHED]:
Expand Down Expand Up @@ -2186,8 +2195,13 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
parents_ids: the ids of all the parent objects in the tree from the current node.
_original_type: If the objects had an original type that was different than what currently exists in the level.t1 and t2
"""
if self._count_diff() is StopIteration:
return
# Pairs forgiven by ignore_nan_inequality are not diffs, so they must
# not consume the max_diffs budget: otherwise enough NaN pairs make
# the traversal stop (with the max-diffs warning) before the real
# diffs are reached, and those diffs are silently dropped.
if not self._is_ignored_nan_inequality(level.t1, level.t2):
if self._count_diff() is StopIteration:
return

if self._use_custom_operator(level):
return
Expand Down Expand Up @@ -2218,7 +2232,7 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
self._report_result('values_changed', level, local_tree=local_tree)
return

if self.ignore_nan_inequality and isinstance(level.t1, (float, np_floating)) and str(level.t1) == str(level.t2) == 'nan':
if self._is_ignored_nan_inequality(level.t1, level.t2):
return

if isinstance(level.t1, booleans):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_diff_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,18 @@ class TestNumpy:
def test_numpy(self, test_name, t1, t2, deepdiff_kwargs, expected_result):
diff = DeepDiff(t1, t2, **deepdiff_kwargs)
assert expected_result == diff, f"test_numpy {test_name} failed."


def test_numpy_ignore_nan_inequality_does_not_consume_max_diffs():
# With ignore_nan_inequality=True, NaN pairs are not diffs and must not
# consume the max_diffs budget; otherwise the traversal stops (with the
# max-diffs warning) before reaching the real diff at the end.
t1 = np.array([np.nan, np.nan, np.nan, np.nan, 1])
t2 = np.array([np.nan, np.nan, np.nan, np.nan, 2])
diff = DeepDiff(t1, t2, ignore_nan_inequality=True, max_diffs=2)
expected = {
'values_changed': {
'root[4]': {'new_value': np.float64(2.0), 'old_value': np.float64(1.0)}
}
}
assert expected == diff
22 changes: 22 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,28 @@ def gen2():
def test_ignore_nan_inequality(self, t1, t2, params, expected_result):
assert expected_result == list(DeepDiff(t1, t2, **params).keys())

@pytest.mark.skipif(pypy3, reason="some versions of pypy3 have nan==nan")
def test_ignore_nan_inequality_does_not_consume_max_diffs(self):
# NaN pairs forgiven by ignore_nan_inequality are not diffs, so they
# must not eat into the max_diffs budget: the real diff after them
# still has to be found, with no max-diffs warning.
nan = float('nan')
t1 = [nan, nan, nan, nan, 1]
t2 = [nan, nan, nan, nan, 2]
expected = {'values_changed': {'root[4]': {'new_value': 2, 'old_value': 1}}}
assert expected == DeepDiff(t1, t2, ignore_nan_inequality=True, max_diffs=2)

t1 = {'a': nan, 'b': nan, 'c': nan, 'd': 1}
t2 = {'a': nan, 'b': nan, 'c': nan, 'd': 2}
expected = {'values_changed': {"root['d']": {'new_value': 2, 'old_value': 1}}}
assert expected == DeepDiff(t1, t2, ignore_nan_inequality=True, max_diffs=2)

def test_max_diffs_still_applies_to_real_diffs(self):
# Sanity check the other direction: real diffs keep counting, so the
# traversal still stops early once the budget is exceeded.
ddiff = DeepDiff([1, 2, 3, 4], [5, 6, 7, 8], max_diffs=2)
assert {'values_changed': {'root[0]': {'new_value': 5, 'old_value': 1}}} == ddiff

@pytest.mark.parametrize('ignore_order, ignore_private_variables, expected', [
(True, True, {}),
(False, True, {}),
Expand Down