Skip to content

Commit e07e8bc

Browse files
authored
Merge pull request #75 from maxrothman/patch-1
Fix not searching in inherited class attributes
2 parents 0d11d08 + 54c7267 commit e07e8bc

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

deepdiff/search.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ def __search_obj(self,
131131
if is_namedtuple:
132132
obj = obj._asdict()
133133
else:
134-
obj = obj.__dict__
134+
# Skip magic methods. Slightly hacky, but unless people are defining
135+
# new magic methods they want to search, it should work fine.
136+
obj = {i: getattr(obj, i) for i in dir(obj)
137+
if not (i.startswith('__') and i.endswith('__'))}
135138
except AttributeError:
136139
try:
137140
obj = {i: getattr(obj, i) for i in obj.__slots__}

tests/test_search.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ def test_case_insensitive_of_str_in_one_liner(self):
286286
result = {'matched_values': {'root'}}
287287
self.assertEqual(DeepSearch(obj, item, verbose_level=1, case_sensitive=False), result)
288288

289+
def test_search_inherited_attributes(self):
290+
class Parent(object):
291+
a = 1
292+
293+
class Child(Parent):
294+
b = 2
295+
296+
obj = Child()
297+
item = 1
298+
result = {'matched_values': {'root.a'}}
299+
self.assertEqual(DeepSearch(obj, item, verbose_level=1), result)
300+
289301

290302
class GrepTestCase(unittest.TestCase):
291303

0 commit comments

Comments
 (0)