Skip to content

Commit

Permalink
diff: fix path_limit with no change (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugebig authored Jun 9, 2023
1 parent 6719335 commit bfb5dd4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dictdiffer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def diff(first, second, node=None, ignore=None, path_limit=None, expand=False,
... path_limit=PathLimit([('a', 'b')])))
[('add', '', [('a', {})]), ('add', 'a', [('b', 'c')])]
>>> from dictdiffer.utils import PathLimit
>>> list(diff({'a': {'b': 'c'}}, {'a': {'b': 'c'}}, path_limit=PathLimit([('a',)])))
[]
The patch can be expanded to small units e.g. when adding multiple values:
>>> list(diff({'fruits': []}, {'fruits': ['apple', 'mango']}))
Expand All @@ -97,6 +101,8 @@ def diff(first, second, node=None, ignore=None, path_limit=None, expand=False,
:param ignore: Set of keys that should not be checked.
:param path_limit: List of path limit tuples or dictdiffer.utils.Pathlimit
object to limit the diff recursion depth.
A diff is still performed beyond the path_limit,
but individual differences will be aggregated up to the path_limit.
:param expand: Expand the patches.
:param tolerance: Threshold to consider when comparing two float numbers.
:param absolute_tolerance: Absolute threshold to consider when comparing
Expand Down Expand Up @@ -203,6 +209,9 @@ def check(key):
# callees again diff function to compare.
# otherwise, the change will be handled as `change` flag.
if path_limit and path_limit.path_is_limit(_node + [key]):
if _first[key] == _second[key]:
return

yield CHANGE, _node + [key], (
deepcopy(_first[key]), deepcopy(_second[key])
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dictdiffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def test_path_limit_deletion(self):
assert res == diffed

def test_path_limit_change(self):
first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
second = {'author': {'last_name': 'Do', 'first_name': 'John'}}
p = PathLimit([('author',)])
diffed = list(diff(first, second, path_limit=p, expand=True))
assert [] == diffed

first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
p = PathLimit([('author',)])
Expand Down

0 comments on commit bfb5dd4

Please sign in to comment.