From 1ae12b9beb98b709f3dd5981f4dc7bcb29c6600e Mon Sep 17 00:00:00 2001 From: tony1233200 <127579663+tony1233200@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:29:49 +0000 Subject: [PATCH] Deepcopy new values when applying a Delta --- deepdiff/delta.py | 16 ++++++++++++---- tests/test_delta.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/deepdiff/delta.py b/deepdiff/delta.py index 5c5702b8..38d63b43 100644 --- a/deepdiff/delta.py +++ b/deepdiff/delta.py @@ -333,6 +333,12 @@ def _set_new_value(self, parent, parent_to_obj_elem, parent_to_obj_action, """ Set the element value on an object and if necessary convert the object to the proper mutable type """ + # The new value comes from the stored diff: copy it so the result + # never aliases the Delta's own values. Otherwise applying the + # same Delta to several objects (the default mutate=False mode) + # links all results together and lets one caller corrupt the + # Delta for the next. + new_value = deepcopy(new_value) if isinstance(obj, tuple): # Check if it's a NamedTuple and use _replace() to generate a new copy with the change if hasattr(obj, '_fields') and hasattr(obj, '_replace'): @@ -677,16 +683,18 @@ def _do_iterable_opcodes(self): # import pytest; pytest.set_trace() obj = self.get_nested_obj(obj=self, elements=elements) is_obj_tuple = isinstance(obj, tuple) - for opcode in opcodes: + for opcode in opcodes: if opcode.tag == 'replace': - # Replace items in list a[i1:i2] with b[j1:j2] - transformed.extend(opcode.new_values) + # Replace items in list a[i1:i2] with b[j1:j2]. + # Copy the new values: they belong to the stored + # diff and must not be aliased into the result. + transformed.extend(deepcopy(opcode.new_values)) elif opcode.tag == 'delete': # Delete items from list a[i1:i2], so we do nothing here continue elif opcode.tag == 'insert': # Insert items from list b[j1:j2] into the new list - transformed.extend(opcode.new_values) + transformed.extend(deepcopy(opcode.new_values)) elif opcode.tag == 'equal': # Items are the same in both lists, so we add them to the result transformed.extend(obj[opcode.t1_from_index:opcode.t1_to_index]) # type: ignore diff --git a/tests/test_delta.py b/tests/test_delta.py index a973706e..85f0d11f 100644 --- a/tests/test_delta.py +++ b/tests/test_delta.py @@ -3058,3 +3058,23 @@ def test_moved_and_type_changed(self): assert [item for item in result if "id" not in item] == [], \ "Phantom entries (dicts missing 'id') found in result" assert result == t2 + + def test_delta_applied_twice_results_are_independent(self): + # Regression test for #655: values inserted by a Delta were + # references into the stored diff, so every application of the + # same Delta shared them. + delta = Delta(DeepDiff({}, {"tags": []})) + left = {} + delta + right = {} + delta + left["tags"].append("left only") + assert right == {"tags": []} + assert {} + delta == {"tags": []} + + def test_delta_type_change_mutable_value_is_copied(self): + # Same aliasing issue through a type change whose new value is + # mutable: editing one result must not leak into the next + # application of the Delta. + delta = Delta(DeepDiff({"a": None}, {"a": {"k": []}})) + first = {"a": None} + delta + first["a"]["k"].append(1) + assert {"a": None} + delta == {"a": {"k": []}}