Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when comparing two nested dicts with 2 added fields #450

Closed
AlejandroSantorum opened this issue Feb 9, 2024 · 5 comments · Fixed by #449
Closed

Error when comparing two nested dicts with 2 added fields #450

AlejandroSantorum opened this issue Feb 9, 2024 · 5 comments · Fixed by #449
Assignees

Comments

@AlejandroSantorum
Copy link

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.

@seperman seperman self-assigned this Apr 5, 2024
@seperman seperman added bug and removed bug labels Apr 5, 2024
@seperman
Copy link
Owner

seperman commented Apr 5, 2024

@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.

@seperman seperman closed this as completed Apr 5, 2024
@AlejandroSantorum
Copy link
Author

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.

@seperman
Copy link
Owner

seperman commented Apr 5, 2024

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

@AlejandroSantorum
Copy link
Author

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.

@seperman
Copy link
Owner

seperman commented Apr 5, 2024

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

@seperman seperman reopened this Apr 5, 2024
@seperman seperman mentioned this issue Apr 5, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants