Open
Description
Please checkout the F.A.Q page before creating a bug ticket to make sure it is not already addressed.
Describe the bug
I cannot get the compare function to work properly on lists that are not 1st-level.
To Reproduce
Define the two following objects. They are identical except that:
- the
version
attribute went0.0.0
->0.0.1
- in the second object has a number field removed in the
numberfield_set
list, and a new one added.UUID4
s are used to track which one should be correlated with which, much likeid
s. in the example in the docs.
self_json= b'{"stringfield_set":[],"numberfield_set":[{"uuid":"fa0c87e8-01f5-43f5-8e63-24886f72ffd0","name":"field_1","address":1,"documentation":"","first_bit_offset":2,"size_in_bits":3,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""},{"uuid":"332e5cfe-886d-41cc-b4d9-0fc1296b3ea0","name":"field_2","address":5,"documentation":"","first_bit_offset":5,"size_in_bits":6,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""}],"enumfield_set":[],"version":"0.0.0","version_date":null,"version_commit":null,"ros_link":null,"documentation":"","definition":42,"information":[],"users":[]}'
other_json=b'{"stringfield_set":[],"numberfield_set":[{"uuid":"fa0c87e8-01f5-43f5-8e63-24886f72ffd0","name":"field_1","address":1,"documentation":"","first_bit_offset":2,"size_in_bits":3,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""},{"uuid":"056429c5-812b-4f49-9aae-1b52bd40aacd","name":"field_3","address":7,"documentation":"","first_bit_offset":8,"size_in_bits":9,"is_signed":false,"is_lsb_left":false,"value_multiply_by":1.0,"value_divide_by":1.0,"value_increase_by":0.0,"value_unit":""}],"enumfield_set":[],"version":"0.0.1","version_date":null,"version_commit":null,"ros_link":null,"documentation":"","definition":42,"information":[],"users":[]}'
make the iterable compare func
def field_diff_function(x, y, level=None):
try:
return x["uuid"] == y["uuid"]
except Exception:
raise CannotCompare() from None
run the compare:
diff_native = DeepDiff(
json.loads(self_json),
json.loads(other_json),
iterable_compare_func=field_diff_function,
ignore_order=True,
)
the following diff is the result:
{
"values_changed": {
"root['numberfield_set'][1]['uuid']": {
"new_value": "056429c5-812b-4f49-9aae-1b52bd40aacd",
"old_value": "332e5cfe-886d-41cc-b4d9-0fc1296b3ea0"
},
"root['numberfield_set'][1]['name']": {
"new_value": "field_3",
"old_value": "field_2"
},
"root['numberfield_set'][1]['address']": {
"new_value": 7,
"old_value": 5
},
"root['numberfield_set'][1]['first_bit_offset']": {
"new_value": 8,
"old_value": 5
},
"root['numberfield_set'][1]['size_in_bits']": {
"new_value": 9,
"old_value": 6
},
"root['version']": {
"new_value": "0.0.1",
"old_value": "0.0.0"
}
}
}
Expected behavior
As the fields should be matched using the uuid
attribute, it should show that one has been added, and the other has been removed, and not that they changed.
OS, DeepDiff version and Python version (please complete the following information):
- Python Version: 3.9.10
- deepdiff version: 5.8.0
Additional context
I'm definitely not ruling out there's a problem somewhere between the chair and the keyboard