Repository: seperman/deepdiff

Issue:
Feature: Search for types/objects
Currently when encountering an object, `DeepSearch` examines that object's `__dict__` without testing whether the object is what's being searched for. By example:

```
>>> from deepdiff import DeepSearch
>>> from uuid import uuid4()
>>> foo = uuid4()
>>> DeepSearch({1: foo}, foo)
{}
```

This would also allow global singletons like `None` and functions to be searched for.

This seems like it'd be a fairly small change, just an `if item == obj` check at the top of `DeepSearch.__search_obj`. If you think this a good idea, I'm happy to make a PR.

Discussion:

Patch:
diff --git a/README.md b/README.md
index 8f5b62f..26ff60e 100644
--- a/README.md
+++ b/README.md
@@ -165,7 +165,7 @@ Let's say you have a huge nested object and want to see if any item with the wor
 ```py
 from deepdiff import DeepSearch
 obj = {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}
-ds = DeepSearch(obj, item, verbose_level=2)
+ds = DeepSearch(obj, "somewhere", verbose_level=2)
 print(ds)
 ```
 
@@ -187,7 +187,7 @@ Just grep through your objects as you would in shell!
 ```py
 from deepdiff import grep
 obj = {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}
-ds = obj | grep(item)
+ds = obj | grep("somewhere")
 print(ds)
 ```
 
diff --git a/deepdiff/__init__.py b/deepdiff/__init__.py
index 6e608d0..95ad688 100644
--- a/deepdiff/__init__.py
+++ b/deepdiff/__init__.py
@@ -1,3 +1,4 @@
+"""This module offers the DeepDiff, DeepSearch, grep and DeepHash classes."""
 import logging
 
 if __name__ == '__main__':
diff --git a/deepdiff/search.py b/deepdiff/search.py
index 26ba882..8c826d8 100644
--- a/deepdiff/search.py
+++ b/deepdiff/search.py
@@ -127,6 +127,11 @@ class DeepSearch(dict):
                      parents_ids=frozenset({}),
                      is_namedtuple=False):
         """Search objects"""
+        found = False
+        if obj == item:
+            found = True
+            self.__report(report_key='matched_values', key=parent, value=obj)
+        
         try:
             if is_namedtuple:
                 obj = obj._asdict()
@@ -136,7 +141,9 @@ class DeepSearch(dict):
             try:
                 obj = {i: getattr(obj, i) for i in obj.__slots__}
             except AttributeError:
-                self['unprocessed'].append("%s" % parent)
+                if not found:
+                    self['unprocessed'].append("%s" % parent)
+                
                 return
 
         self.__search_dict(


Test patch:
diff --git a/tests/test_search.py b/tests/test_search.py
index 49929c6..3f56ceb 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -15,6 +15,7 @@ To run a specific test, run this from the root of repo:
 """
 import unittest
 from deepdiff import DeepSearch, grep
+from datetime import datetime
 import logging
 logging.disable(logging.CRITICAL)
 
@@ -286,6 +287,30 @@ class DeepSearchTestCase(unittest.TestCase):
         result = {'matched_values': {'root'}}
         self.assertEqual(DeepSearch(obj, item, verbose_level=1, case_sensitive=False), result)
 
+    def test_none(self):
+        obj = item = None
+        result = {'matched_values': {'root'}}
+        self.assertEqual(DeepSearch(obj, item, verbose_level=1), result)
+
+    def test_complex_obj(self):
+        obj  = datetime(2017, 5, 4, 1, 1, 1)
+        item = datetime(2017, 5, 4, 1, 1, 1)
+        result = {'matched_values': {'root'}}
+        self.assertEqual(DeepSearch(obj, item, verbose_level=1), result)
+
+    def test_keep_searching_after_obj_match(self):
+        class AlwaysEqual:
+            def __init__(self, recurse=True):
+                if recurse:
+                    self.some_attr = AlwaysEqual(recurse=False)
+            def __eq__(self, other):
+                return True
+
+        obj = AlwaysEqual()
+        item = AlwaysEqual()
+        result = {'matched_values': {'root', 'root.some_attr'}}
+        self.assertEqual(DeepSearch(obj, item, verbose_level=1), result)
+
 
 class GrepTestCase(unittest.TestCase):