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

Removing an item from a list causes a cascade of "changes", rather than "removes" #182

Open
leodking opened this issue Feb 8, 2024 · 1 comment
Labels

Comments

@leodking
Copy link

leodking commented Feb 8, 2024

Package version (if known): 0.8.1

I try diffing two YAML files - in the second one, I've removed a single item from the list. Ideally I would like to see this as a remove diff. Instead I get:

[('change', ['targ1', 0], ('one', 'two')), ('change', ['targ1', 1], ('two', 'three')), ('remove', 'targ1', [(2, 'three')])]

So basically it thinks I've changed "one" to "two", "two" to "three", and then removed "three".

It works if I remove an item from the end of a list, but I can't guarantee that this will be the case. I'm using ruamel.yaml to load the dicts from YAML

Test code:

from dictdiffer import diff as dictdiff
from ruamel import yaml

first = """
targ1:
    - one
    - two
    - three

"""

second = """
targ1:
    - two
    - three
"""

first_d = yaml.load(first, Loader=yaml.Loader)
second_d = yaml.load(second, Loader=yaml.Loader)

res = dictdiff(first_d, second_d)
diffs = list(res)
print(diffs)
@leodking leodking added the bug label Feb 8, 2024
@leodking
Copy link
Author

leodking commented Feb 8, 2024

Oh, this also affects "adds". Basically if I insert an item into a list it causes a cascade of changes rather than additions:

first = """
targ1:
    - one
    - two
    - three
    - four
    - five
"""

second = """
targ1:
    - one
    - two
    - three
    - threepointfive
    - four
    - five
"""

Result:

[('change', ['targ1', 3], ('four', 'threepointfive')), ('change', ['targ1', 4], ('five', 'four')), ('add', 'targ1', [(5, 'five')])]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant