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

DeepDiff fails when dictionary keys are unrepresentable and exclude_regex_paths is not None

nleehone opened this issue · comments

Describe the bug
The path for dictionary keys that are not Python literals is set to None. This causes issues when exclude_regex_paths is set.

To Reproduce
Run the following script:


import deepdiff


@dataclass(frozen=True)
class Id:
    val: int


# Show that the repr properly represents the object
print(Id(2).__repr__())

d1 = {
    Id(1): 10,
    Id(2): 20,
}

d2 = {
    Id(1): 10,
    Id(2): 10,
}

print(deepdiff.DeepDiff(d1, d2, exclude_regex_paths=["any"]))

The traceback is

Id(val=2)
stringify_param was not able to get a proper repr for "Id(val=2)". This object will be reported as None. Add instructions for this object to DeepDiff's helper.literal_eval_extended to make it work properly: malformed node or string: <ast.Call object at 0x000001E071388C40>
Traceback (most recent call last):
  File "scratch.py", line 24, in <module>
    print(deepdiff.DeepDiff(d1, d2, exclude_regex_paths=["any"]))
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 320, in __init__
    self._diff(root, parents_ids=frozenset({id(t1)}), _original_type=_original_type)
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 1562, in _diff
    self._diff_dict(level, parents_ids, local_tree=local_tree)
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 616, in _diff_dict
    self._diff(next_level, parents_ids_added, local_tree=local_tree)
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 1526, in _diff
    if self._skip_this(level):
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 477, in _skip_this
    [exclude_regex_path.search(level_path) for exclude_regex_path in self.exclude_regex_paths]):
  File "py3.9\lib\site-packages\deepdiff\diff.py", line 477, in <listcomp>
    [exclude_regex_path.search(level_path) for exclude_regex_path in self.exclude_regex_paths]):
TypeError: expected string or bytes-like object

Expected behavior
The DeepDiff should not fail when exclude_regex_paths is set, and should return the same results as when it is not specified if none of the paths match.

There is a parameter in stringify_param called force which seems to be meant to deal with this issue, but that parameter is never used:

def stringify_param(self, force=None):

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

  • OS: Windows
  • Version 10
  • Python Version: 3.9.13
  • DeepDiff Version: 6.7.1