This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kevinbenton
Recipients kevinbenton, michael.foord
Date 2014-12-18.08:45:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1418892323.1.0.910322233262.issue23078@psf.upfronthosting.co.za>
In-reply-to
Content
If one of the mock.patch methods is used with autospec=True on a staticmethod in an object, the mock library determines that it is not callable by checking for the __call__ attribute. This results in a NonCallableMagicMock being returned which of course dies with the following error when the mocked method is called:

TypeError: 'NonCallableMagicMock' object is not callable


It seems that the create_autospec needs to special case for classmethod and staticmethod.



The following change seems to fix it, however I am only vaguely familiar with the internals of mock so I'm not sure what this breaks.

diff -r d356250e275d mock.py
--- a/mock.py	Tue Apr 09 14:53:33 2013 +0100
+++ b/mock.py	Wed Dec 17 07:35:15 2014 -0800
@@ -2191,7 +2191,8 @@
         # descriptors don't have a spec
         # because we don't know what type they return
         _kwargs = {}
-    elif not _callable(spec):
+    elif not _callable(spec) and not isinstance(spec, (staticmethod,
+                                                       classmethod)):
         Klass = NonCallableMagicMock
     elif is_type and instance and not _instance_callable(spec):
         Klass = NonCallableMagicMock
History
Date User Action Args
2014-12-18 08:45:23kevinbentonsetrecipients: + kevinbenton, michael.foord
2014-12-18 08:45:23kevinbentonsetmessageid: <1418892323.1.0.910322233262.issue23078@psf.upfronthosting.co.za>
2014-12-18 08:45:22kevinbentonlinkissue23078 messages
2014-12-18 08:45:21kevinbentoncreate