deepdiff
deepdiff copied to clipboard
Correctly mark objects as deleted or added
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"}}
}
@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.
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?
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
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!
Hello,I have not had a chance to look at it yet.We will need to keep track of the portion of the nested object that is intact vs. is different and based on that decide if we should report the object as modified or removed/added.PRs are very welcome!Thanks,Sep DehpourOn Nov 2, 2023, at 8:58 AM, devin13cox @.***> wrote: 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. We are running into essentially the same exact flow as @Martin25699. Is anything in progress or have any reliable workarounds been found? Thanks!
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>