Skip to content

Different behavior when a list has only one item #489

Open

Description

@dtphan

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

Activity

  1. bongi23 commented on Feb 10, 2025

    @bongi23

    Identical problem here!

  2. OnlineBunker commented on Nov 13, 2026

    @OnlineBunker

    Reproduced on current dev with AI assistance. The intersection cutoff skips pairing before the custom matcher runs. Setting cutoff_intersection_for_pairs=1 gives the expected field-level change and addition.

    Would a tested fix letting an explicit matcher bypass that heuristic be welcome, or should the cutoff keep taking precedence? I can work on it if that direction fits.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions