deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

adding attribute and pickling causes failure when subtracting delta

Open dlarosa11 opened this issue 6 months ago • 4 comments

When I pickle the diff and then unpickle and try to subtract delta created from diff, I get error. I added prinst statements to your code starting at 'Line 571 of delta.py'.

Code:

from deepdiff import DeepDiff, Delta

def execute_with_pickle(t1, t2):
    print(f"t1: {t1}")
    print(f"t2: {t2}")
    diff = DeepDiff(t1, t2)
    jsoned = diff.to_json_pickle()
    diff_new = DeepDiff.from_json_pickle(jsoned)
    delta: Delta = Delta(diff_new, bidirectional=True)
    t3 = t2 - delta
    print(f"t3: {t3}")

def execute_without_pickle(t1, t2):
    print(f"t1: {t1}")
    print(f"t2: {t2}")
    diff = DeepDiff(t1, t2)
    delta: Delta = Delta(diff, bidirectional=True)
    t3 = t2 - delta
    print(f"t3: {t3}")

print("GOOD with pickle")
t1 = {1: 1, 2: 2, 3: 3}
t2 = {1: 1, 2: "2", 3: 3}
execute_with_pickle(t1, t2)

print("---------")

print("GOOD without pickle")
t1 = {1: 1, 2: 2, 3: 3}
t2 = {1: 1, 2: "2", 3: 3}
execute_without_pickle(t1, t2)

print("---------")

print("GOOD without pickle")
t1 = {1: 1, 2: 2, 3: 3}
t2 = {1: 1, 2: 2, 3: 3, 4: 4}
execute_without_pickle(t1, t2)

print("---------")

print("ERROR with pickle")
t1 = {1: 1, 2: 2, 3: 3}
t2 = {1: 1, 2: 2, 3: 3, 4: 4}
execute_with_pickle(t1, t2)

Output

GOOD with pickle
t1: {1: 1, 2: 2, 3: 3}
t2: {1: 1, 2: '2', 3: 3}
t3: {1: 1, 2: 2, 3: 3}
---------
GOOD without pickle
t1: {1: 1, 2: 2, 3: 3}
t2: {1: 1, 2: '2', 3: 3}
t3: {1: 1, 2: 2, 3: 3}
---------
GOOD without pickle
t1: {1: 1, 2: 2, 3: 3}
t2: {1: 1, 2: 2, 3: 3, 4: 4}
Line 571 of delta.py
type(items): <class 'dict'>
items: {'root[4]': 4}
t3: {1: 1, 2: 2, 3: 3}
---------
ERROR with pickle
t1: {1: 1, 2: 2, 3: 3}
t2: {1: 1, 2: 2, 3: 3, 4: 4}
Line 571 of delta.py
type(items): <class 'deepdiff.model.PrettyOrderedSet'>
items: [root[4]]
Traceback (most recent call last):
  File "/Users/domeniclarosa/ws/virtual_envs/myenv.deepdiff/lib/python3.9/site-packages/deepdiff/delta.py", line 574, in _do_item_removed
    sorted_item = sorted(items.items(), key=self._sort_key_for_item_added, reverse=True)
TypeError: 'list' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/domeniclarosa/ws/work/dlc/experimental/deepdifferror.py", line 45, in <module>
    execute_with_pickle(t1, t2)
  File "/Users/domeniclarosa/ws/work/dlc/experimental/deepdifferror.py", line 10, in execute_with_pickle
    t3 = t2 - delta
  File "/Users/domeniclarosa/ws/virtual_envs/myenv.deepdiff/lib/python3.9/site-packages/deepdiff/delta.py", line 209, in __rsub__
    result = self.__add__(other)
  File "/Users/domeniclarosa/ws/virtual_envs/myenv.deepdiff/lib/python3.9/site-packages/deepdiff/delta.py", line 192, in __add__
    self._do_dictionary_item_removed()
  File "/Users/domeniclarosa/ws/virtual_envs/myenv.deepdiff/lib/python3.9/site-packages/deepdiff/delta.py", line 681, in _do_dictionary_item_removed
    self._do_item_removed(dictionary_item_removed)
  File "/Users/domeniclarosa/ws/virtual_envs/myenv.deepdiff/lib/python3.9/site-packages/deepdiff/delta.py", line 576, in _do_item_removed
    sorted_item = sorted(items.items(), key=cmp_to_key(self._sort_comparison), reverse=True)
TypeError: 'list' object is not callable

dlarosa11 avatar Aug 20 '24 19:08 dlarosa11