Skip to content

Commit 80de733

Browse files
committed
Fix for iterable moved items what are found with iterable_compare_func.
To stay consistent with other types of reporting, moved items should be relative to t2. Also, moved items should branch deeper to look for more nested changes (similar to item added and removed).
1 parent be22027 commit 80de733

File tree

2 files changed

+58
-61
lines changed

2 files changed

+58
-61
lines changed

deepdiff/diff.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -862,30 +862,6 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
862862

863863
else: # check if item value has changed
864864

865-
# if (i != j):
866-
# # Item moved
867-
# change_level = level.branch_deeper(
868-
# x,
869-
# y,
870-
# child_relationship_class=child_relationship_class,
871-
# child_relationship_param=i,
872-
# child_relationship_param2=j
873-
# )
874-
# self._report_result('iterable_item_moved', change_level)
875-
876-
# item_id = id(x)
877-
# if parents_ids and item_id in parents_ids:
878-
# continue
879-
# parents_ids_added = add_to_frozen_set(parents_ids, item_id)
880-
881-
# # Go one level deeper
882-
# next_level = level.branch_deeper(
883-
# x,
884-
# y,
885-
# child_relationship_class=child_relationship_class,
886-
# child_relationship_param=j)
887-
# self._diff(next_level, parents_ids_added)
888-
889865
if (i != j and ((x == y) or self.iterable_compare_func)):
890866
# Item moved
891867
change_level = level.branch_deeper(
@@ -896,20 +872,22 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
896872
child_relationship_param2=j
897873
)
898874
self._report_result('iterable_item_moved', change_level, local_tree=local_tree)
899-
continue
900875

901876
item_id = id(x)
902877
if parents_ids and item_id in parents_ids:
903878
continue
904879
parents_ids_added = add_to_frozen_set(parents_ids, item_id)
905880

906881
# 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.
907885
next_level = level.branch_deeper(
908886
x,
909887
y,
910888
child_relationship_class=child_relationship_class,
911-
child_relationship_param=i,
912-
child_relationship_param2=j,
889+
child_relationship_param=j,
890+
child_relationship_param2=i
913891
)
914892
self._diff(next_level, parents_ids_added, local_tree=local_tree)
915893

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
11
{
2-
"dictionary_item_added": [
3-
"root['Cars'][3]['dealers']"
4-
],
5-
"dictionary_item_removed": [
6-
"root['Cars'][3]['production']"
7-
],
8-
"values_changed": {
9-
"root['Cars'][3]['model']": {
10-
"new_value": "Supra",
11-
"old_value": "supra"
12-
}
2+
"dictionary_item_added": [
3+
"root['Cars'][3]['dealers']"
4+
],
5+
"dictionary_item_removed": [
6+
"root['Cars'][3]['production']"
7+
],
8+
"values_changed": {
9+
"root['Cars'][2]['dealers'][0]['quantity']": {
10+
"new_value": 50,
11+
"old_value": 20
1312
},
14-
"iterable_item_added": {
15-
"root['Cars'][0]": {
16-
"id": "7",
17-
"make": "Toyota",
18-
"model": "8Runner"
19-
}
13+
"root['Cars'][1]['model_numbers'][2]": {
14+
"new_value": 3,
15+
"old_value": 4
16+
},
17+
"root['Cars'][3]['model']": {
18+
"new_value": "Supra",
19+
"old_value": "supra"
20+
}
21+
},
22+
"iterable_item_added": {
23+
"root['Cars'][2]['dealers'][1]": {
24+
"id": 200,
25+
"address": "200 Fake St",
26+
"quantity": 10
27+
},
28+
"root['Cars'][1]['model_numbers'][3]": 4,
29+
"root['Cars'][0]": {
30+
"id": "7",
31+
"make": "Toyota",
32+
"model": "8Runner"
33+
}
34+
},
35+
"iterable_item_removed": {
36+
"root['Cars'][2]['dealers'][0]": {
37+
"id": 103,
38+
"address": "103 Fake St",
39+
"quantity": 50
2040
},
21-
"iterable_item_removed": {
22-
"root['Cars'][1]": {
23-
"id": "2",
24-
"make": "Toyota",
25-
"model": "Highlander",
26-
"dealers": [
27-
{
28-
"id": 123,
29-
"address": "123 Fake St",
30-
"quantity": 50
31-
},
32-
{
33-
"id": 125,
34-
"address": "125 Fake St",
35-
"quantity": 20
36-
}
37-
]
41+
"root['Cars'][1]": {
42+
"id": "2",
43+
"make": "Toyota",
44+
"model": "Highlander",
45+
"dealers": [
46+
{
47+
"id": 123,
48+
"address": "123 Fake St",
49+
"quantity": 50
50+
},
51+
{
52+
"id": 125,
53+
"address": "125 Fake St",
54+
"quantity": 20
3855
}
56+
]
3957
}
58+
}
4059
}

0 commit comments

Comments
 (0)