question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mock patch autospec doesn't work on staticmethods

See original GitHub issue

cPython bug 23078

This is a copy of the bug I filed on the python issuetracker here[1]. I 
apologize if I filed in the wrong place. I'm not sure which is the correct 
tracker for this library.


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





1. http://bugs.python.org/issue23078

Original issue reported on code.google.com by blak...@gmail.com on 18 Dec 2014 at 8:48

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

8reactions
rsyringcommented, Jun 6, 2017

Could the patch mentioned above get merged without waiting for upstream? I just hit this bug.

1reaction
cjw296commented, Apr 30, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

unittest.mock patch autospec doesn't work on staticmethods
Hi, I hit this problem wile mocking one static method and found this fix. Tested it and works for me. However, I did...
Read more >
Mocking a module function called by a static method does not ...
It seems that your patch does not apply in your test - the function you are mocking is only mocked in the setUp...
Read more >
[issue23078] unittest.mock patch autospec doesn't work on ...
It seems that the create_autospec needs to special case for classmethod and staticmethod. The following change seems to fix it, however I am ......
Read more >
Python Mocking: A Guide to Better Unit Tests - Toptal
This comprehensive guide to Python mocking will help you keep your unit tests straightforward and explain how to best use the mock.unittest library...
Read more >
Understanding the Python Mock Object Library - Real Python
unittest.mock provides a powerful mechanism for mocking objects, called patch() , which looks up an object in a given module and replaces that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found