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

Check equality of precompiled regex patterns #336

Closed
cohml opened this issue Aug 11, 2022 · 5 comments
Closed

Check equality of precompiled regex patterns #336

cohml opened this issue Aug 11, 2022 · 5 comments
Assignees
Labels

Comments

@cohml
Copy link

cohml commented Aug 11, 2022

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.

@seperman
Copy link
Owner

@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.

@cohml
Copy link
Author

cohml commented Aug 15, 2022

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
Copy link
Author

cohml commented Aug 7, 2023

@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!

@seperman
Copy link
Owner

seperman commented Aug 7, 2023

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
Copy link
Owner

seperman commented Sep 1, 2023

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

@seperman seperman closed this as completed Sep 1, 2023
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