Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterable_compare_func not working properly #414

Open
brxdeop opened this issue Aug 31, 2023 · 6 comments
Open

iterable_compare_func not working properly #414

brxdeop opened this issue Aug 31, 2023 · 6 comments
Assignees
Labels

Comments

@brxdeop
Copy link

brxdeop commented Aug 31, 2023

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
@brxdeop brxdeop changed the title terable_compare_func not working properly iterable_compare_func not working properly Aug 31, 2023
@seperman seperman added the bug label Aug 31, 2023
@brxdeop
Copy link
Author

brxdeop commented 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 and pip 22.1.1.

  • with deepdiff 6.4.0 - Another Bug like mentioned on an other issue

    >>> from deepdiff import DeepDiff
    
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/usr/lib/python3.10/site-packages/deepdiff/__init__.py", line 10, in <module>
         from .diff import DeepDiff
       File "/usr/lib/python3.10/site-packages/deepdiff/diff.py", line 19, in <module>
         from deepdiff.helper import (strings, bytes_type, numbers, uuids, datetimes, ListItemRemovedOrAdded, notpresent,
       File "/usr/lib/python3.10/site-packages/deepdiff/helper.py", line 158, in <module>
         if get_semvar_as_integer(np.__version__) < 1019000:
     AttributeError: 'NoneType' object has no attribute '__version__'
     >>> from deepdiff.helper import CannotCompare
     Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/usr/lib/python3.10/site-packages/deepdiff/__init__.py", line 10, in <module>
         from .diff import DeepDiff
       File "/usr/lib/python3.10/site-packages/deepdiff/diff.py", line 19, in <module>
         from deepdiff.helper import (strings, bytes_type, numbers, uuids, datetimes, ListItemRemovedOrAdded, notpresent,
       File "/usr/lib/python3.10/site-packages/deepdiff/helper.py", line 158, in <module>
         if get_semvar_as_integer(np.__version__) < 1019000:
     AttributeError: 'NoneType' object has no attribute '__version__'
  • with deepdiff 6.3.1, 6.3.0, 6.2.3, 6.2.2, 6.2.1

    >>> DeepDiff(t1, t2, iterable_compare_func=compare_func)
    {}
  • with deepdiff 5.8.1 - working

    >>> DeepDiff(t1, t2, iterable_compare_func=compare_func)
    {'iterable_item_added': {"root[2]['value'][2]": 1}, 'iterable_item_removed': {"root[1]['value'][2]": 1}}

@brxdeop
Copy link
Author

brxdeop commented 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 on t2[index_K] even in the case that these two items have different id, DeepDiff reports this as a value change.

@wolverdude
Copy link

+1 I am also affected.

@jessbringlarsen
Copy link

+1 same

@KashyapGovindu
Copy link

@seperman Is there any info on this bug?

@seperman
Copy link
Owner

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!

@seperman seperman self-assigned this Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants