I am facing recently an issue regarding the compare function 'iterable_compare_func'. Basically deepdiff return a wrong diff.
I test with exactly the same two lists described in the documentation and the result is not the same.
from deepdiff import DeepDiff
from deepdiff.helper import CannotCompare
t1 = [
{
'id': 1,
'value': [1]
},
{
'id': 2,
'value': [7, 8, 1]
},
{
'id': 3,
'value': [7, 8],
},
]
t2 = [
{
'id': 2,
'value': [7, 8]
},
{
'id': 3,
'value': [7, 8, 1],
},
{
'id': 1,
'value': [1]
},
]
>>> DeepDiff(t1, t2)
{'values_changed': {"root[0]['id']": {'new_value': 2, 'old_value': 1}, "root[0]['value'][0]": {'new_value': 7, 'old_value': 1}, "root[1]['id']": {'new_value': 3, 'old_value': 2}, "root[2]['id']": {'new_value': 1, 'old_value': 3}, "root[2]['value'][0]": {'new_value': 1, 'old_value': 7}}, 'iterable_item_added': {"root[0]['value'][1]": 8}, 'iterable_item_removed': {"root[2]['value'][1]": 8}}
iterable_compare_func.
def compare_func(x, y, level=None):
try:
return x['id'] == y['id']
except Exception:
raise CannotCompare() from None
now by setting the function. the result is wrong as below.
>>> DeepDiff(t1, t2, iterable_compare_func=compare_func)
{}
>>> DeepDiff(t1, t2, iterable_compare_func=compare_func, verbose_level=2)
{'iterable_item_moved': {'root[0]': {'new_path': 'root[2]', 'value': {'id': 1, 'value': [1]}}, 'root[1]': {'new_path': 'root[0]', 'value': {'id': 2, 'value': [7, 8]}}, 'root[2]': {'new_path': 'root[1]', 'value': {'id': 3, 'value': [7, 8, 1]}}}}
The issue is encountered with deepdiff v6.3.1 and the same issue is validated within 2 different environments. If anyone had the same issue or can reproduce to confirm.
Thank you.
Expected is as what the documentation describes : iterable_compare_func helps to define what elements to compare to others and here we define that the elements with same IDs should be compared with each other.