Repository: kaste/mockito-python

Issue:
Issue unstubbing random.randint
When trying to monkeypatch random.randint and then later unstubbing the original method does not get reinstated. Example:

```
import sys
sys.version
Out[3]: '3.10.4 (main, Mar 31 2022, 03:38:35) [Clang 12.0.0 ]'
import random
import mockito
mockito.__version__
Out[6]: '1.2.2'
random.randint(1, 10)
Out[7]: 6
mockito.when(random).randint(...).thenReturn("Mocked!")
Out[8]: <mockito.invocation.AnswerSelector at 0x7ff076012c80>
random.randint(1, 10)
Out[9]: 'Mocked!'
mockito.unstub()
random.randint(1, 10)
Traceback (most recent call last):
...
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-a5705e42ffa6>", line 1, in <cell line: 1>
    random.randint(1, 10)
AttributeError: module 'random' has no attribute 'randint'`
```

This seems to be because: 

not inspect.isclass(self.mocked_obj) and inspect.ismethod(original_method) 

in the method restore_method within mocking returns True when it should be False.


Discussion:

Patch:
diff --git a/mockito/mocking.py b/mockito/mocking.py
index e52db5e..7ac5c8c 100644
--- a/mockito/mocking.py
+++ b/mockito/mocking.py
@@ -180,8 +180,11 @@ class Mock(object):
         # the one on its class, so we delete as well.
         if (
             not original_method
-            or not inspect.isclass(self.mocked_obj)
-            and inspect.ismethod(original_method)
+            or (
+                inspect.ismethod(original_method)
+                and not inspect.isclass(self.mocked_obj)
+                and not inspect.ismodule(self.mocked_obj)
+            )
         ):
             delattr(self.mocked_obj, method_name)
         else:


Test patch:
diff --git a/tests/modulefunctions_test.py b/tests/modulefunctions_test.py
index 6d8728f..d1429e1 100644
--- a/tests/modulefunctions_test.py
+++ b/tests/modulefunctions_test.py
@@ -98,3 +98,10 @@ class ModuleFunctionsTest(TestBase):
         from . import module
         when(module).Foo().thenReturn('mocked')
         assert module.Foo() == 'mocked'
+
+    def testUnstubFunctionOnModuleWhichIsActuallyAMethod_issue_53(self):
+        import random
+        when(random).randint(...).thenReturn("mocked")
+        assert random.randint(1, 10) == "mocked"
+        unstub(random)
+        assert random.randint(1, 10) != "mocked"