deepdiff
deepdiff copied to clipboard
Check equality of precompiled regex patterns
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 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.
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 😉
@seperman Is this issue still unresolved? If so, can you:
- assign it to me
- 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!
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
@cohml Thanks for taking care of this ticket. DeepDiff 6.4.0 is released and includes your work! https://zepworks.com/deepdiff/current/