deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Check equality of precompiled regex patterns

Open cohml opened this issue 3 years ago • 2 comments

DeepDiff does not currently seem able to detect differences between precompiled regex patterns.

>>> from deepdiff import DeepDiff
>>> import re
>>> pattern_1 = re.compile('foo')
>>> pattern_2 = re.compile('bar')
>>> DeepDiff(pattern_1, pattern_1)  # expected
{}
>>> DeepDiff(pattern_1, pattern_2)  # unexpected
{}

I'm not sure if this behavior is unintended (then it's a bug) or not (then it's a feature request). The docs don't seem to reference it. Either way, the output above certainly is not what you'd expect.

Note that regular assert appears able to detect this difference just fine:

>>> assert pattern_1 == pattern_1  # expected
>>> assert pattern_1 == pattern_2  # expected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

Tested using:

$ python --version
Python 3.8.13
$ python -c "import deepdiff; print(deepdiff.__version__)"
5.8.1

At a minimum, DeepDiff should return information on how the patterns themselves differ, akin to the following comparison:

>>> DeepDiff(pattern_1.pattern, pattern_2.pattern)
{'values_changed': {'root': {'new_value': 'bar', 'old_value': 'foo'}}}

Ideally, it would also be great if all attributes of the two Pattern objects could be compared, such as their flags. This may be more challenging, though.

cohml avatar Aug 11 '22 19:08 cohml

@cohml DeepDiff needs some dev work to detect changes in precompiled regex objects. Thanks for reporting it. If you would like to make a PR to address the issue, you are very welcome to do so. Otherwise I will take a look at it when I have a chance in the next couple of months.

seperman avatar Aug 14 '22 06:08 seperman

Thanks for the prompt reply @seperman. I will definitely check this issue out in the near future if I too can find the time. It is near and dear to my heart 😉

cohml avatar Aug 15 '22 17:08 cohml

@seperman Is this issue still unresolved? If so, can you:

  1. assign it to me
  2. provide any starter code, or at least pointers on where to start looking into the source code, to implement the logic for detecting changes in precompiled regex

Thanks!

cohml avatar Aug 07 '23 15:08 cohml

Hi @cohml Wow, it has been a year since your comment. If you have time to look into it, that would be great. Thank you!

You can add an if condition that checks whether level.t1 is a pre-compiled regex and then report on it: https://github.com/seperman/deepdiff/blob/master/deepdiff/diff.py#L1527-L1561

We should also make sure that the Delta object will be able to reproduce these changes: https://github.com/seperman/deepdiff/blob/master/deepdiff/delta.py#L384

seperman avatar Aug 07 '23 18:08 seperman

@cohml Thanks for taking care of this ticket. DeepDiff 6.4.0 is released and includes your work! https://zepworks.com/deepdiff/current/

seperman avatar Sep 01 '23 00:09 seperman