Skip to content

Commit f8397c7

Browse files
committed
fixes a bug when _get_clean_to_keys_mapping would be called without
explicit significant digits
1 parent e4b59cf commit f8397c7

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

deepdiff/diff.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,11 @@ def _get_clean_to_keys_mapping(self, keys, level):
600600
clean_key = key.value
601601
elif isinstance(key, numbers):
602602
type_ = "number" if self.ignore_numeric_type_changes else key.__class__.__name__
603-
clean_key = self.number_to_string(key, significant_digits=self.significant_digits,
604-
number_format_notation=self.number_format_notation)
603+
if self.significant_digits is None:
604+
clean_key = key
605+
else:
606+
clean_key = self.number_to_string(key, significant_digits=self.significant_digits,
607+
number_format_notation=self.number_format_notation)
605608
clean_key = KEY_TO_VAL_STR.format(type_, clean_key)
606609
else:
607610
clean_key = key

tests/test_delta.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,10 @@ def test_delta_set_in_objects(self):
18351835
def test_delta_array_of_bytes(self):
18361836
t1 = []
18371837
t2 = [b"hello"]
1838-
delta = Delta(DeepDiff(t1, t2))
1838+
diff = DeepDiff(t1, t2)
1839+
expected_diff = {'iterable_item_added': {'root[0]': b'hello'}}
1840+
assert expected_diff == diff
1841+
delta = Delta(diff)
18391842
flat_result = delta.to_flat_rows()
18401843
flat_expected = [FlatDeltaRow(path=[0], action=FlatDataAction.iterable_item_added, value=b'hello', type=bytes)]
18411844
assert flat_expected == flat_result

tests/test_diff_math.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,17 @@ def test_math_diff_ignore_order_warning(self, caplog):
110110
}
111111
assert res == expected
112112
# assert "math_epsilon will be ignored." in caplog.text
113+
114+
def test_ignore_numeric_type_changes_with_numeric_keys_and_no_significant_digits(self):
115+
"""Test that ignore_numeric_type_changes works with numeric keys when significant_digits is None.
116+
117+
This test covers the bug fix in _get_clean_to_keys_mapping where significant_digits=None
118+
caused a crash when number_to_string was called without the required parameter.
119+
"""
120+
# Test case with numeric keys and ignore_numeric_type_changes=True, significant_digits=None
121+
d1 = {1: "value1", 2.5: "value2"}
122+
d2 = {1.0: "value1", 2.5: "value2"} # int vs float keys
123+
124+
# This should not crash and should treat 1 and 1.0 as the same key
125+
result = DeepDiff(d1, d2, ignore_numeric_type_changes=True, significant_digits=None)
126+
assert result == {}

0 commit comments

Comments
 (0)