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

Correctly mark objects as deleted or added #290

Open
Martin25699 opened this issue Jan 13, 2022 · 5 comments
Open

Correctly mark objects as deleted or added #290

Martin25699 opened this issue Jan 13, 2022 · 5 comments

Comments

@Martin25699
Copy link

Martin25699 commented Jan 13, 2022

How to correctly mark objects as deleted or added?

def iterable_compare_func(x, y, level=None):
    try:
        return x["id"] == y["id"]
    except Exception:
        raise CannotCompare() from None

old_data = [
        {
            "id": 4,
            "name": "boo"
        },
        {
            "id": 2,
            "name": "foo"
        },
    ]
new_data = [
        {
            "id": 2,
            "name": "foo"
        },
        {
            "id": 3,
            "name": "bar"
        },
    ]
result = DeepDiff(old_data, new_data, ignore_order=True, iterable_compare_func=iterable_compare_func).to_json()
print(result)

Actual result:

{
    "values_changed": {
        "root[1]['id']": {"new_value": 3, "old_value": 4},
        "root[1]['name']": {"new_value": "bar", "old_value": "boo"}
    }
}

Expected Result:

{
"iterable_item_removed": {"root[1]": {"id": 4, "name": "boo"}},
"iterable_item_added": {"root[1]": {"id": 3, "name": "bar"}}
}
@seperman
Copy link
Owner

@Martin25699 This needs some dev work. I'm not sure when I will have time to do it. PR's are always welcome! Basically when comparing dictionaries, if every single key is the same but some threshold of values are different, we should just report that the whole dictionary is added or removed.

@LizardBlizzard
Copy link

I think this issue extends beyond dictionaries. I am doing something similar with objects and also getting "values_changed" instead of "iterable_item_added" and "iterable_item_removed" entries:

from pprint import pprint
from deepdiff import DeepDiff

class Child:
    def __init__(self, id:int , value: str) -> None:
        self.id = id
        self.value = value

class Parent:
    def __init__(self, id:int , children: list[Child]) -> None:
        self.id = id
        self.children = children

parent_v1 = Parent(id=1, children=[Child(id=2, value="child1")])
parent_v2 = Parent(id=1, children=[Child(id=3, value="child2")])
diff = DeepDiff(parent_v1, parent_v2)
pprint(diff)

Results:

{'values_changed': {'root.children[0].id': {'new_value': 3, 'old_value': 2},
                    'root.children[0].value': {'new_value': 'child2',
                                               'old_value': 'child1'}}}

@seperman Do you think it's the same issue?

@wvargas-ods
Copy link

Too sad, this feature should be coded ASAP, I wasted a lot of hours, thinking that my code was wrong 🥲
At least it should be in the Docs, so everyone can read it and understand the limitation that iterable_compare_func has right now

@devin13cox
Copy link

devin13cox commented Nov 2, 2023

Hello everyone! I wanted to check in and see if there was any work being done on this issue. I am new to this repo but I can try taking a look myself. Happy to add our specific use case, but we are running into essentially the same exact flow as @Martin25699. Is anything in progress or have any reliable workarounds been found? Thanks!

@seperman
Copy link
Owner

seperman commented Nov 4, 2023 via email

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

No branches or pull requests

5 participants