-
-
Notifications
You must be signed in to change notification settings - Fork 246
Description
Please checkout the F.A.Q page before creating a bug ticket to make sure it is not already addressed.
Describe the bug
the issue is related to the use of the ability to compare iterable element with a customized function.
with an iterable_compare_func
defined as in the documentation : to compare iterables only if they have the same id, the result of deepdiff is wrong as it is missing the differences.
To Reproduce
Exactly with the same examples on documentation.
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]
},
]
def compare_func(x, y, level=None):
try:
return x['id'] == y['id']
except Exception:
raise CannotCompare() from None
without iterable function - result is as expected
>>>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}}
without iterable function - result is wrong
>>> 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]}}}}
>>> DeepDiff(t1, t2, iterable_compare_func=compare_func, cutoff_distance_for_pairs=1, cutoff_intersection_for_pairs=1)
{}
as you can see on verbosity_level=2 the element is marked as moved even if it is not even the same value ? how this could happen ?
Expected behavior
Expected behavior to list compare with ids so the change will be.
>>>DeepDiff(t1, t2, iterable_compare_func=compare_func)
{'iterable_item_added': {"root[2]['value'][2]": 1}, 'iterable_item_removed': {"root[1]['value'][2]": 1}}
OS, DeepDiff version and Python version (please complete the following information):
The tests has been performed on several environments.
- OS: Debian, SilverBlue (Fedora), Container (Alpine)
- Python Version 3.10, 3.11.4, 3.10 respectively
- DeepDiff Version : 6.3.1
Activity
[-]terable_compare_func not working properly[/-][+]iterable_compare_func not working properly[/+]brxdeop commentedon Sep 1, 2023
Hello.
Just to add a tracking for the behavior on several deepdiff versions.
The test are done inside a container with
python 3.10.5
andpip 22.1.1
.with deepdiff 6.4.0 - Another Bug like mentioned on an other issue
with deepdiff 6.3.1, 6.3.0, 6.2.3, 6.2.2, 6.2.1
with deepdiff 5.8.1 - working
brxdeop commentedon Sep 6, 2023
Hello @seperman
Is there any plan regarding a fix.
I want to mention that even with the previous working version (
v5.8.1
) there some cases where the iterable_compare_function is not working properly.For example, when an iterable item is deleted on
t1[index_K]
and another iterable item is added ont2[index_K]
even in the case that these two items have differentid
, DeepDiff reports this as a value change.wolverdude commentedon Oct 12, 2023
+1 I am also affected.
jessbringlarsen commentedon Nov 2, 2023
+1 same
KashyapGovindu commentedon Apr 15, 2024
@seperman Is there any info on this bug?
seperman commentedon Apr 16, 2024
Hello, thanks for reminding me. I have not had a chance to take a look at it yet. I will keep you posted once I fix it. PRs are always very welcome!
dtorres-sf commentedon Jul 25, 2024
I have opened a PR that fixes the issue. With the PR here are the outputs for the initial test case:
Note: I believe 6.1.0 is the latest version that will not have this bug.
wolverdude commentedon Jan 24, 2025
I believe this issue is fixed. PR #473 was merged last year, and the fix debuted in DeepDiff 8.0.0.
dtorres-sf commentedon Jan 25, 2025
@wolverdude Yes, this is fixed. This ticket can be closed.