Skip to content

Commit 8676729

Browse files
committed
1. Improved comment in delta.py:556-558 — now references the path inversion mechanism in model.py
DiffLevel.path so future developers understand why the keys are swapped. 2. Added test_moved_and_type_changed — covers the type_changes code path where items both move and change type (str -> int), which the original tests didn't exercise.
1 parent 6eb95f6 commit 8676729

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

deepdiff/delta.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,9 @@ def _get_elements_and_details(self, path):
553553
def _do_values_or_type_changed(self, changes, is_type_change=False, verify_changes=True):
554554
compare_func_was_used = self.diff.get('_iterable_compare_func_was_used', False)
555555
for path, value in changes.items():
556-
# When iterable_compare_func is used, keys in values_changed/type_changes are
557-
# t2 paths and new_path holds the original t1 path. Always apply at t1 so we
556+
# When iterable_compare_func is used, DiffLevel.path() inverts use_t2 for
557+
# moved items (see model.py DiffLevel.path). This means dict keys here are
558+
# actually t2 paths and new_path holds the t1 path. Apply at t1 so we
558559
# don't access indices that don't exist yet or modify the wrong item.
559560
apply_path = value['new_path'] if (compare_func_was_used and value.get('new_path')) else path
560561
elem_and_details = self._get_elements_and_details(apply_path)

tests/test_delta.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2993,4 +2993,20 @@ def test_appended_only_no_movement_sanity_check(self):
29932993
result = t1 + Delta(ddiff, force=True, raise_errors=True,
29942994
log_errors=False)
29952995
assert [item for item in result if "id" not in item] == []
2996-
assert result == t2
2996+
assert result == t2
2997+
2998+
def test_moved_and_type_changed(self):
2999+
"""Items that both move and change type should apply correctly."""
3000+
t1 = [{"id": "a", "val": "1"}, {"id": "b", "val": "2"}]
3001+
t2 = [
3002+
{"id": "new1", "val": 99},
3003+
{"id": "a", "val": 1}, # moved [0]->[1], val str->int
3004+
{"id": "b", "val": 2}, # moved [1]->[2], val str->int
3005+
]
3006+
ddiff = DeepDiff(t1, t2, iterable_compare_func=self.compare_func,
3007+
threshold_to_diff_deeper=0)
3008+
result = t1 + Delta(ddiff, force=True, raise_errors=True,
3009+
log_errors=False)
3010+
assert [item for item in result if "id" not in item] == [], \
3011+
"Phantom entries (dicts missing 'id') found in result"
3012+
assert result == t2

0 commit comments

Comments
 (0)