pythonpython-deepdiff

DeepDiff exclude_paths regex not filtering out paths


I have two dictionaries I'd like to compare using DeepDiff, excluding the "a" key in the "items" list:

d1 = {"items": [{"a": 1, "b": 2}, {"a": 1, "b": 2}]}
d2 = {"items": [{"a": 10, "b": 2}, {"a": 100, "b": 2}]}

exclude_paths = [r"root\['items'\]\[\d+\]\['a'\]"]
diff = DeepDiff(d1, d2, exclude_paths=exclude_paths)
pprint(diff)

Ideally DeepDiff should report an empty dictionary but it's reporting the "a" values.

{'values_changed': {"root['items'][0]['a']": {'new_value': 10, 'old_value': 1},
                    "root['items'][1]['a']": {'new_value': 100,
                                              'old_value': 1}}}

Am I doing anything wrong in my exlude_paths regex? I'm following the docs.

I've tried others values for exlude_paths but no luck:

r"root['items'][\d+]['a']"
r"root\[\'items\'\]\[\d\]\[\'a\'\]"

Solution

  • Just needed to use the exclude_regex_paths argument instead of the exclude_paths argument:

    d1 = {"items": [{"a": 1, "b": 2}, {"a": 1, "b": 2}]}
    d2 = {"items": [{"a": 10, "b": 2}, {"a": 100, "b": 2}]}
    
    diff = DeepDiff(d1, d2, exclude_regex_paths=[r"root\['items'\]\[\d+\]\['a'\]"])
    pprint(diff)  # {}