Skip to content

Commit 11eec09

Browse files
committed
adding - v4-0-8: Adding ignore_nan_inequality for float('nan')
1 parent 1058251 commit 11eec09

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ And then running
417417

418418
# ChangeLog
419419

420+
- v4-0-8: Adding ignore_nan_inequality for float('nan')
420421
- v4-0-7: Hashing of the number 1 vs. True
421422
- v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround.
422423
- v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func.

deepdiff/diff.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(self,
5959
ignore_type_subclasses=False,
6060
ignore_string_case=False,
6161
number_to_string_func=None,
62+
ignore_nan_inequality=False,
6263
verbose_level=1,
6364
view=TEXT_VIEW,
6465
hasher=None,
@@ -69,7 +70,7 @@ def __init__(self,
6970
"The valid parameters are ignore_order, report_repetition, significant_digits, "
7071
"number_format_notation, exclude_paths, exclude_types, exclude_regex_paths, ignore_type_in_groups, "
7172
"ignore_string_type_changes, ignore_numeric_type_changes, ignore_type_subclasses, "
72-
"number_to_string_func, verbose_level, view, and hasher.") % ', '.join(kwargs.keys()))
73+
"ignore_nan_inequality, number_to_string_func, verbose_level, view, and hasher.") % ', '.join(kwargs.keys()))
7374

7475
self.ignore_order = ignore_order
7576
self.ignore_type_in_groups = self.get_ignore_types_in_groups(
@@ -88,6 +89,7 @@ def __init__(self,
8889
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
8990
self.ignore_string_case = ignore_string_case
9091
self.number_to_string = number_to_string_func or number_to_string
92+
self.ignore_nan_inequality = ignore_nan_inequality
9193
self.hashes = {}
9294
self.hasher = hasher
9395

@@ -613,6 +615,9 @@ def __diff(self, level, parents_ids=frozenset({})):
613615
self.__diff_types(level)
614616
return
615617

618+
if self.ignore_nan_inequality and isinstance(level.t1, float) and str(level.t1) == str(level.t2) == 'nan':
619+
return
620+
616621
if isinstance(level.t1, strings):
617622
self.__diff_str(level)
618623

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Indices and tables
281281
Changelog
282282
=========
283283

284+
- v4-0-8: Adding ignore_nan_inequality for float('nan')
284285
- v4-0-7: Hashing of the number 1 vs. True
285286
- v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround.
286287
- v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func.

tests/test_diff_text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,3 +1723,11 @@ def test_bool_vs_number(self):
17231723

17241724
ddiff = DeepDiff(t1, t2, ignore_order=True)
17251725
assert ddiff == {}
1726+
1727+
@pytest.mark.parametrize('t1, t2, params, expected_result', [
1728+
(float('nan'), float('nan'), {}, ['values_changed']),
1729+
(float('nan'), float('nan'), {'ignore_nan_inequality': True}, []),
1730+
([1, float('nan')], [1, float('nan')], {'ignore_nan_inequality': True}, []),
1731+
])
1732+
def test_ignore_nan_inequality(self, t1, t2, params, expected_result):
1733+
assert expected_result == list(DeepDiff(t1, t2, **params).keys())

0 commit comments

Comments
 (0)