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

Fix for iterable moved items what are found with iterable_compare_func #473

Merged
merged 4 commits into from
Aug 27, 2024

Conversation

dtorres-sf
Copy link
Contributor

Our usage of deepdiff heavily involves iterable_compare_func and we have been unable to upgrade past 6.1.0 because of an issue that was introduced in this changeset.

There are two key changes:

  1. When an item is moved, rather than stopping the current iteration, continue to go one level deeper the way item added and removed do.
  2. Purposely make moved items relative to t2 rather than t1. This keeps it consistent with other change types. This was originally introduced with this changeset but later got changed around.

This PR should fix this issue:

#414

seperman and others added 2 commits April 8, 2024 16:57
To stay consistent with other types of reporting, moved items
should be relative to t2. Also, moved items should branch deeper
to look for more nested changes (similar to item added and removed).
@dtorres-sf
Copy link
Contributor Author

I will work on getting unittests fixed soon which may require making some of my changes conditional on either a new flag or iterable_compare_func

@seperman seperman changed the base branch from master to dev July 25, 2024 16:45
Copy link
Owner

@seperman seperman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dtorres-sf
Thanks for making the PR.
I may be missing something. What is the benefit of going one level deeper if the item is "moved"? Is it because you want the flexibility when using iterable_compare_func ?
Usually when we report an object as "moved", it means nothing has changed about it except its path, so there is no more need to go deeper.

@dtorres-sf
Copy link
Contributor Author

dtorres-sf commented Jul 29, 2024

@seperman we still need to go deeper, here is an example a basic example:

    {
        '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]
    },
]

In the above example we have both moved top level items as well as changed the moved items. For iterable_compare_func you an object can move but still have changes at the same time.

With these changes here is the output:

{'iterable_item_added': {"root[1]['value'][2]": 1}, 'iterable_item_removed': {"root[0]['value'][2]": 1}, '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]}}}}

@dtorres-sf
Copy link
Contributor Author

@seperman I fixed the unittest by gating one of my changes to only be used for iterable_compare_func. Also updating unittests to be correct.

You can see more examples of why we need to branch deeper in the unittests updates:

https://github.com/seperman/deepdiff/pull/473/files#diff-88c1a4e0a49a73c7956ac53687c9f9d5d710e22516fb76e5773f531ee41cb4acR1956-R1961

These unittests were originally written with the branch deeper in mind - this shows that iterable_compare_func used to go deeper and show the nested values being changed:

'values_changed': {"root[2]['val']": {'new_value': 3, 'old_value': 1}},

@seperman
Copy link
Owner

seperman commented Aug 6, 2024

@dtorres-sf Thanks for fixing the tests. Do you think we should add a parameter that allows the user to decide whether we should go deeper when using iterable_compare_func or not?

@dtorres-sf
Copy link
Contributor Author

@seperman We could, I am trying to think of the use cases of when they would not want to go deeper. If they don't go deeper they will see that items moved, but not the changes that happened inside the moved items. This effectively misses some of the differences deepdiff is used to find. Do you remember why that continue was added to early exit before going deeper?

The changeset that made the change mentions using difflib but not sure why stopping the branch deeper was added as part of that.

@dtorres-sf
Copy link
Contributor Author

@seperman Just wanted to follow up here to see if this is a change that you think we could move forward with? If you think it is best to add a parameter for these changes I am fine with that and can get that added.

@seperman
Copy link
Owner

Hi @dtorres-sf
Sorry for the delay in responding. I'm reviewing your PR now.

@seperman
Copy link
Owner

@dtorres-sf I think that was added to make it consistent with the rest of the flow where moving means the object has not changed.

Copy link
Owner

@seperman seperman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge it and add the continue part below when we are not using iterable compare func.

# If the item was moved using an iterable_compare_func then we want to make sure that the index
# is relative to t2.
reference_param1 = j
reference_param2 = i
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it continue if not self.iterable_compare_func

@seperman seperman merged commit fe70fa0 into seperman:dev Aug 27, 2024
2 of 5 checks passed
@seperman
Copy link
Owner

seperman commented Aug 27, 2024

@dtorres-sf DeepDiff 8.0.0 is published and it includes your contribution. Thank you!

@dtorres-sf
Copy link
Contributor Author

@seperman Thank you so much!! I will try it out and let you know if there are any issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants