diff --git a/CHANGELOG.md b/CHANGELOG.md index 9282c421..3c3a0106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # DeepDiff Change log - Unreleased + - Fixed `DeepSearch(exclude_types=...)` applying type exclusions to the search term instead of the current object. Excluded root and nested objects are now skipped, while excluding the search term's type no longer suppresses matches on other object types (#629). - Fixed missing type changes between equal-comparing items inside ordered iterables, e.g. `DeepDiff([2], [2.0])` now reports the `int` → `float` change like `DeepDiff(2, 2.0)` and `DeepDiff({'a': 2}, {'a': 2.0})` already do (issue #605). - v9-1-0 diff --git a/deepdiff/search.py b/deepdiff/search.py index 9b1b11a1..79e0d9c1 100644 --- a/deepdiff/search.py +++ b/deepdiff/search.py @@ -319,7 +319,7 @@ def __search_tuple(self, obj: Tuple[Any, ...], item: Any, parent: str, parents_i def __search(self, obj: Any, item: Any, parent: str = "root", parents_ids: FrozenSet[int] = frozenset()) -> None: """The main search method""" - if self.__skip_this(item, parent): + if self.__skip_this(obj, parent): return elif isinstance(obj, strings) and isinstance(item, (strings, RE_COMPILED_TYPE)): diff --git a/tests/test_search.py b/tests/test_search.py index 3984349a..e5e67f30 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -260,6 +260,21 @@ def test_skip_type_str(self): ds = DeepSearch(obj, item, verbose_level=1, exclude_types=[str]) assert ds == result + @pytest.mark.parametrize('obj', [[item], (item,), {'key': item}, CustomClass(item)]) + def test_skip_root_object_type(self, obj): + assert DeepSearch(obj, item, exclude_types=[type(obj)]) == {} + + @pytest.mark.parametrize('excluded', [[item], (item,), CustomClass(item)]) + def test_skip_dictionary_value_type(self, excluded): + obj = {'skip': excluded, 'keep': item} + result = {'matched_values': {"root['keep']"}} + assert DeepSearch(obj, item, exclude_types=[type(excluded)]) == result + + def test_excluding_item_type_does_not_skip_other_object_types(self): + obj = {'needle': 1, 'value': 'needle'} + result = {'matched_paths': {"root['needle']"}} + assert DeepSearch(obj, 'needle', exclude_types=[str]) == result + def test_skip_regexp(self): obj = [{'a': 1, 'b': "somewhere"}, {'c': 4, 'b': "somewhere"}] ds = DeepSearch(obj, item, exclude_regex_paths=[r"root\[\d+\]"])