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

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

Open
nleehone opened this issue Dec 7, 2023 · 0 comments
Assignees
Labels

Comments

@nleehone
Copy link

nleehone commented Dec 7, 2023

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
@seperman seperman added the bug label May 14, 2024
@seperman seperman self-assigned this May 14, 2024
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

2 participants