deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Different behavior when a list has only one item

Open dtphan opened this issue 1 year ago • 1 comments

Please checkout the F.A.Q page before creating a bug ticket to make sure it is not already addressed.

Describe the bug

To Reproduce

def compare_func(x, y, level=None):
    return x["id"] == y["id"]

# First test case
A = {'array': [{'id': 1, 'x': 'a1'}, {'id': 2, 'x': 'b1'}]}
B = {'array': [{'id': 1, 'x': 'a1'}, {'id': 2, 'x': 'b0'}, {'id': 3, 'x': 'c1'}]}
DeepDiff(A, B, ignore_order=True, report_repetition=True, verbose_level=2, iterable_compare_func=compare_func)
# {'values_changed': {"root['array'][1]['x']": {'new_value': 'b0', 'old_value': 'b1'}}, 
# 'iterable_item_added': {"root['array'][2]": {'id': 3, 'x': 'c1'}}}

# → 1 changed, 1 added: Good and expected

# Second test case
A = {'array': [{'id': 1, 'x': 'a1'}]}
B = {'array': [{'id': 1, 'x': 'a0'}, {'id': 2, 'x': 'b1'}]}
DeepDiff(A, B, ignore_order=True, report_repetition=True, verbose_level=2, iterable_compare_func=compare_func)
# {'iterable_item_added': {"root['array'][0]": {'id': 1, 'x': 'a0'}, "root['array'][1]": {'id': 2, 'x': 'b1'}}, 
# 'iterable_item_removed': {"root['array'][0]": {'id': 1, 'x': 'a1'}}}

# → 2 added, 1 removed: unexpected result
# moreover, the compare_func was not executed at all

# if I remove report_repetition=True, it kind of works but still does not used the specified compare_func
DeepDiff(A, B, ignore_order=True, verbose_level=2, iterable_compare_func=compare_func)
# {'values_changed': {"root['array'][0]": {'new_value': {'id': 1, 'x': 'a0'}, 'old_value': {'id': 1, 'x': 'a1'}}}, 
#  'iterable_item_added': {"root['array'][1]": {'id': 2, 'x': 'b1'}}}

Expected behavior I expected the second test case to return one 'values_changed' and one 'iterable_item_added', e.g.,

{'values_changed': {"root['array'][0]['x']": {'new_value': 'a0', 'old_value': 'a1'}}, 'iterable_item_added': {"root['array'][1]": {'id': 2, 'x': 'b1'}}}

The compare_func should also be called in the second test case

OS, DeepDiff version and Python version (please complete the following information):

  • OS: Windows
  • Version 11
  • Python Version 3.10.6
  • DeepDiff Version 8.0.1

dtphan avatar Sep 20 '24 14:09 dtphan

Identical problem here!

bongi23 avatar Feb 10 '25 12:02 bongi23