Skip to content

Check equality of precompiled regex patterns #336

@cohml

Description

@cohml

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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions