Skip to content

Commit bfb5dd4

Browse files
authored
diff: fix path_limit with no change (#174)
1 parent 6719335 commit bfb5dd4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

dictdiffer/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def diff(first, second, node=None, ignore=None, path_limit=None, expand=False,
7676
... path_limit=PathLimit([('a', 'b')])))
7777
[('add', '', [('a', {})]), ('add', 'a', [('b', 'c')])]
7878
79+
>>> from dictdiffer.utils import PathLimit
80+
>>> list(diff({'a': {'b': 'c'}}, {'a': {'b': 'c'}}, path_limit=PathLimit([('a',)])))
81+
[]
82+
7983
The patch can be expanded to small units e.g. when adding multiple values:
8084
8185
>>> list(diff({'fruits': []}, {'fruits': ['apple', 'mango']}))
@@ -97,6 +101,8 @@ def diff(first, second, node=None, ignore=None, path_limit=None, expand=False,
97101
:param ignore: Set of keys that should not be checked.
98102
:param path_limit: List of path limit tuples or dictdiffer.utils.Pathlimit
99103
object to limit the diff recursion depth.
104+
A diff is still performed beyond the path_limit,
105+
but individual differences will be aggregated up to the path_limit.
100106
:param expand: Expand the patches.
101107
:param tolerance: Threshold to consider when comparing two float numbers.
102108
:param absolute_tolerance: Absolute threshold to consider when comparing
@@ -203,6 +209,9 @@ def check(key):
203209
# callees again diff function to compare.
204210
# otherwise, the change will be handled as `change` flag.
205211
if path_limit and path_limit.path_is_limit(_node + [key]):
212+
if _first[key] == _second[key]:
213+
return
214+
206215
yield CHANGE, _node + [key], (
207216
deepcopy(_first[key]), deepcopy(_second[key])
208217
)

tests/test_dictdiffer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ def test_path_limit_deletion(self):
207207
assert res == diffed
208208

209209
def test_path_limit_change(self):
210+
first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
211+
second = {'author': {'last_name': 'Do', 'first_name': 'John'}}
212+
p = PathLimit([('author',)])
213+
diffed = list(diff(first, second, path_limit=p, expand=True))
214+
assert [] == diffed
215+
210216
first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
211217
second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
212218
p = PathLimit([('author',)])

0 commit comments

Comments
 (0)