From 23117e3d81b30e448f07c17793d04ed0c7008bd1 Mon Sep 17 00:00:00 2001 From: Mate Valko Date: Mon, 18 Nov 2024 12:03:43 +0100 Subject: [PATCH] The Delta class was failing to properly deserialize opcodes when they were in list format from JSON. Added handling for both dictionary and list format opcodes during deserialization. List format opcodes contain [tag, t1_from, t1_to, t2_from, t2_to, old_values, new_values]. This fixes test failures in test_list_of_alphabet_and_its_delta when using JSON serialization. --- deepdiff/delta.py | 21 +++++++++++++++++---- tests/test_delta.py | 6 +++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/deepdiff/delta.py b/deepdiff/delta.py index 8bafc9a6..d2b1dd8e 100644 --- a/deepdiff/delta.py +++ b/deepdiff/delta.py @@ -98,11 +98,24 @@ def _deserializer(obj, safe_to_import=None): for path, op_codes in result['_iterable_opcodes'].items(): _iterable_opcodes[path] = [] for op_code in op_codes: - _iterable_opcodes[path].append( - Opcode( - **op_code + if isinstance(op_code, list): + # Handle list format: [tag, t1_from, t1_to, t2_from, t2_to, old_values, new_values] + _iterable_opcodes[path].append( + Opcode( + tag=op_code[0], + t1_from_index=op_code[1], + t1_to_index=op_code[2], + t2_from_index=op_code[3], + t2_to_index=op_code[4], + old_values=op_code[5], + new_values=op_code[6] + ) + ) + else: + # Handle dict format + _iterable_opcodes[path].append( + Opcode(**op_code) ) - ) result['_iterable_opcodes'] = _iterable_opcodes return result diff --git a/tests/test_delta.py b/tests/test_delta.py index 81a05784..2a945581 100644 --- a/tests/test_delta.py +++ b/tests/test_delta.py @@ -2436,9 +2436,9 @@ def test_list_of_alphabet_and_its_delta(self): flat_rows = delta2.to_flat_rows() expected_flat_rows = [ - FlatDeltaRow(path=[3], action='values_changed', value='X', old_value='D', type=str, old_type=str, new_path=[2]), - FlatDeltaRow(path=[6], action='values_changed', value='Z', old_value='G', type=str, old_type=str), - FlatDeltaRow(path=[5], action='values_changed', value='Y', old_value='F', type=str, old_type=str), + FlatDeltaRow(path=[3], action=FlatDataAction.values_changed, value='X', old_value='D', type=str, old_type=str, new_path=[2]), + FlatDeltaRow(path=[6], action=FlatDataAction.values_changed, value='Z', old_value='G', type=str, old_type=str), + FlatDeltaRow(path=[5], action=FlatDataAction.values_changed, value='Y', old_value='F', type=str, old_type=str), FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_deleted, value=[], old_value=['A'], type=list, old_type=list, t1_from_index=0, t1_to_index=1, t2_from_index=0, t2_to_index=0), FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_equal, value=None, old_value=None, type=type(None), old_type=type(None), t1_from_index=1, t1_to_index=3, t2_from_index=0, t2_to_index=2), FlatDeltaRow(path=[], action=FlatDataAction.iterable_items_replaced, value=['X'], old_value=['D', 'E', 'F', 'G'], type=list, old_type=list, t1_from_index=3, t1_to_index=7, t2_from_index=2, t2_to_index=3),