seperman / deepdiff

DeepDiff: Deep Difference and search of any Python object/data. DeepHash: Hash of any object based on its contents. Delta: Use deltas to reconstruct objects by adding deltas together.

Home Page:http://zepworks.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when comparing two nested dicts with 2 added fields

AlejandroSantorum opened this issue · comments

Describe the bug
DeepDiff fails when 2 new fields are added in t2 in different nested dictionary fields. The library identifies correctly the name of the new fields, but it is unable to retrieve the correct dictionary levels or paths to them.
DeepDiff works as expected when just a new field is added, but it breaks when 2 new fields are added in different levels.

To Reproduce
Create two dictionaries to compare:

s1 = {
    "type": "struct",
    "fields": [
        {"name": "Competition", "metadata": {}, "nullable": True, "type": "string"},
        {"name": "TeamName", "metadata": {}, "nullable": True, "type": "string"},
        {
            "name": "Contents",
            "metadata": {},
            "nullable": True,
            "type": {
                "type": "struct",
                "fields": [
                    {"name": "Date", "metadata": {}, "nullable": True, "type": "string"},
                    {"name": "Player1", "metadata": {}, "nullable": True, "type": "string"}
                ]
            }
        }
    ]
}

s2 = {
    "type": "struct",
    "fields": [
        {"name": "Competition", "metadata": {}, "nullable": True, "type": "string"},
        {"name": "GlobalId", "metadata": {}, "nullable": True, "type": "string"},
        {"name": "TeamName", "metadata": {}, "nullable": True, "type": "string"},
        {
            "name": "Contents",
            "metadata": {},
            "nullable": True,
            "type": {
                "type": "struct",
                "fields": [
                    {"name": "Date", "metadata": {}, "nullable": True, "type": "string"},
                    {"name": "Player1", "metadata": {}, "nullable": True, "type": "string"},
                    {"name": "Player2", "metadata": {}, "nullable": True, "type": "string"}
                ]
            }
        }
    ]
}

The s2 dictionary contains two new fields GlobalId at root['fields'][1] and Player2 at root['fields'][3]['type']['fields'][2], but if I run

dd = DeepDiff(t1=s1, t2=s2, ignore_order=True, verbose_level=2)

I get:

{
    'iterable_item_added': {
        "root['fields'][1]": {'name': 'GlobalId', 'metadata': {}, 'nullable': True, 'type': 'string'},
        "root['fields'][2]['type']['fields'][2]": {'name': 'Player2', 'metadata': {}, 'nullable': True, 'type': 'string'}
    }
}

Which is wrong for the second added field: the new field name is correctly identified, but the path within the dictionary is wrong, the path root['fields'][2]['type']['fields'][2] does not exist.

Expected behavior
It should return the right dictionary path for the second added field, that is root['fields'][3]['type']['fields'][2], i.e., the full correct output should be:

{
    'iterable_item_added': {
        "root['fields'][1]": {'name': 'GlobalId', 'metadata': {}, 'nullable': True, 'type': 'string'},
        "root['fields'][3]['type']['fields'][2]": {'name': 'Player2', 'metadata': {}, 'nullable': True, 'type': 'string'}
    }
}

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

  • OS: Linux
  • Version: Any
  • Python Version 3.8
  • DeepDiff Version > 6.5.0 (tested with 6.5.1 and 6.7.1).

Additional context
None.

@AlejandroSantorum
This is not a bug. All the paths point to your s1.
In this case what it is saying, is that to your s1['fields'][2]['type']['fields'], a 3rd item (index 2) is added.

Dear @seperman,

Thank you for the reply and apologies for the misunderstanding.

Is there any way to get the paths of the added items pointing to t2 instead of t1?

Many thanks.

No worries! There is a bug that the path for t2 is not shown correctly. I am working on it.

Thank you for your time. If you can post here a comment when the path in t2 is fixed I would be highly grateful.

Appreciate it.

Sure, I have fixed it for your case in the dev branch. I need to look into more examples.