Skip to content

Commit fc8baaa

Browse files
author
Joachim Langenbach
committed
Fixed include_paths fault, if only certain keys of a path are included
1 parent 916f02f commit fc8baaa

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

deepdiff/diff.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,32 @@ def _skip_this(self, level):
510510

511511
return skip
512512

513+
def _skip_this_key(self, level, key):
514+
# if include_paths is not set, than treet every path as included
515+
if self.include_paths is None:
516+
return False
517+
if "{}['{}']".format(level.path(), key) in self.include_paths:
518+
return False
519+
if level.path() in self.include_paths:
520+
# matches e.g. level+key root['foo']['bar']['veg'] include_paths ["root['foo']['bar']"]
521+
return False
522+
for prefix in self.include_paths:
523+
if "{}['{}']".format(level.path(), key) in prefix:
524+
# matches as long the prefix is longer than this object key
525+
# eg.: level+key root['foo']['bar'] matches prefix root['foo']['bar'] from include paths
526+
# level+key root['foo'] matches prefix root['foo']['bar'] from include_paths
527+
# level+key root['foo']['bar'] DOES NOT match root['foo'] from include_paths This needs to be handled afterwards
528+
return False
529+
# check if a higher level is included as a whole (=without any sublevels specified)
530+
# matches e.g. level+key root['foo']['bar']['veg'] include_paths ["root['foo']"]
531+
# but does not match, if it is level+key root['foo']['bar']['veg'] include_paths ["root['foo']['bar']['fruits']"]
532+
up = level.up
533+
while up is not None:
534+
if up.path() in self.include_paths:
535+
return False
536+
up = up.up
537+
return True
538+
513539
def _get_clean_to_keys_mapping(self, keys, level):
514540
"""
515541
Get a dictionary of cleaned value of keys to the keys themselves.
@@ -570,11 +596,11 @@ def _diff_dict(
570596
rel_class = DictRelationship
571597

572598
if self.ignore_private_variables:
573-
t1_keys = SetOrdered([key for key in t1 if not(isinstance(key, str) and key.startswith('__'))])
574-
t2_keys = SetOrdered([key for key in t2 if not(isinstance(key, str) and key.startswith('__'))])
599+
t1_keys = SetOrdered([key for key in t1 if not(isinstance(key, str) and key.startswith('__')) and not self._skip_this_key(level, key)])
600+
t2_keys = SetOrdered([key for key in t2 if not(isinstance(key, str) and key.startswith('__')) and not self._skip_this_key(level, key)])
575601
else:
576-
t1_keys = SetOrdered(t1.keys())
577-
t2_keys = SetOrdered(t2.keys())
602+
t1_keys = SetOrdered([key for key in t1 if not self._skip_this_key(level, key)])
603+
t2_keys = SetOrdered([key for key in t2 if not self._skip_this_key(level, key)])
578604
if self.ignore_string_type_changes or self.ignore_numeric_type_changes or self.ignore_string_case:
579605
t1_clean_to_keys = self._get_clean_to_keys_mapping(keys=t1_keys, level=level)
580606
t2_clean_to_keys = self._get_clean_to_keys_mapping(keys=t2_keys, level=level)

0 commit comments

Comments
 (0)