Skip to content

Commit 33def72

Browse files
committed
Only swap i and j when reporting items moved if using iterable_compare_func.
Fix unittests to represent the branching deeper
1 parent 80de733 commit 33def72

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

deepdiff/diff.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -840,13 +840,15 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
840840
if self._count_diff() is StopIteration:
841841
return # pragma: no cover. This is already covered for addition.
842842

843+
reference_param1 = i
844+
reference_param2 = j
843845
if y is ListItemRemovedOrAdded: # item removed completely
844846
change_level = level.branch_deeper(
845847
x,
846848
notpresent,
847849
child_relationship_class=child_relationship_class,
848-
child_relationship_param=i,
849-
child_relationship_param2=j,
850+
child_relationship_param=reference_param1,
851+
child_relationship_param2=reference_param2,
850852
)
851853
self._report_result('iterable_item_removed', change_level, local_tree=local_tree)
852854

@@ -855,8 +857,8 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
855857
notpresent,
856858
y,
857859
child_relationship_class=child_relationship_class,
858-
child_relationship_param=i,
859-
child_relationship_param2=j,
860+
child_relationship_param=reference_param1,
861+
child_relationship_param2=reference_param2,
860862
)
861863
self._report_result('iterable_item_added', change_level, local_tree=local_tree)
862864

@@ -868,26 +870,30 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
868870
x,
869871
y,
870872
child_relationship_class=child_relationship_class,
871-
child_relationship_param=i,
872-
child_relationship_param2=j
873+
child_relationship_param=reference_param1,
874+
child_relationship_param2=reference_param2
873875
)
874876
self._report_result('iterable_item_moved', change_level, local_tree=local_tree)
875877

878+
if self.iterable_compare_func:
879+
# Intentionally setting j as the first child relationship param in cases of a moved item.
880+
# If the item was moved using an iterable_compare_func then we want to make sure that the index
881+
# is relative to t2.
882+
reference_param1 = j
883+
reference_param2 = i
884+
876885
item_id = id(x)
877886
if parents_ids and item_id in parents_ids:
878887
continue
879888
parents_ids_added = add_to_frozen_set(parents_ids, item_id)
880889

881890
# Go one level deeper
882-
# Intentionally setting j as the first child relationship param in cases of a moved item.
883-
# If the item was moved using an iterable_compare_func then we want to make sure that the index
884-
# is relative to t2.
885891
next_level = level.branch_deeper(
886892
x,
887893
y,
888894
child_relationship_class=child_relationship_class,
889-
child_relationship_param=j,
890-
child_relationship_param2=i
895+
child_relationship_param=reference_param1,
896+
child_relationship_param2=reference_param2
891897
)
892898
self._diff(next_level, parents_ids_added, local_tree=local_tree)
893899

tests/test_delta.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,14 @@ def test_compare_func_with_duplicates_removed(self):
18791879
"val": 3
18801880
}
18811881
}
1882-
}
1882+
},
1883+
'values_changed': {
1884+
"root[2]['val']": {
1885+
'new_value': 3,
1886+
'old_value': 1,
1887+
'new_path': "root[0]['val']"
1888+
}
1889+
},
18831890
}
18841891
assert expected == ddiff
18851892
delta = Delta(ddiff)
@@ -1888,6 +1895,7 @@ def test_compare_func_with_duplicates_removed(self):
18881895

18891896
flat_result = delta.to_flat_rows()
18901897
flat_expected = [
1898+
{'path': [2, 'val'], 'value': 3, 'action': 'values_changed', 'type': int, 'new_path': [0, 'val']},
18911899
{'path': [2], 'value': {'id': 1, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict},
18921900
{'path': [0], 'value': {'id': 1, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict},
18931901
{'path': [3], 'value': {'id': 3, 'val': 3}, 'action': 'iterable_item_removed', 'type': dict},
@@ -1930,6 +1938,12 @@ def test_compare_func_with_duplicates_removed(self):
19301938
'val': 3
19311939
}
19321940
}
1941+
},
1942+
'values_changed': {
1943+
"root[2]['val']": {
1944+
'new_value': 3,
1945+
'new_path': "root[0]['val']"
1946+
}
19331947
}
19341948
}
19351949
assert expected_delta_dict == delta_again.diff
@@ -1961,7 +1975,14 @@ def test_compare_func_with_duplicates_added(self):
19611975
'val': 1
19621976
}
19631977
}
1964-
}
1978+
},
1979+
'values_changed': {
1980+
"root[0]['val']": {
1981+
'new_value': 1,
1982+
'old_value': 3,
1983+
'new_path': "root[2]['val']"
1984+
}
1985+
},
19651986
}
19661987
assert expected == ddiff
19671988
delta = Delta(ddiff)

0 commit comments

Comments
 (0)