-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unittest.mock patch autospec doesn't work on staticmethods #67267
Comments
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 |
Here's a patch which does this. One problem could be that staticmethod(some_callable) or classmethod(some_callable) aren't callable per se, but given the fact that users expects Foo.staticmethod or Foo.klassmethod to be callable when patching them, it's something we should consider for fixing. |
Looks like b6ea3dc89a78 is not a valid changeset. Could you attach a patch from the Mercurial repo? |
Ups, sorry about that, I'll update the patch. |
Regarding Claudiu's comment about Separately, would it be better to include the check for |
The attached patch implements these changes through _callable instead. This patch also ensures that the underlying object that staticmethod and classmethod wrap is a callable object as well. |
Hi, I hit this problem wile mocking one static method and found this fix. Is there any reason why it is not pushed anywhere, yet? |
I planned to upgrade Chromium OS's mock module to 2.0.0. I also encountered the issue that classmethod cannot be patched as callable mock. Reviewers, can we start the review process? |
This affects all versions from 3.4 up to 3.8-dev. Would be nice if someone could do the review of the supplied patch. Thanks for awesome work on Python! I'm here because it just hit me also and I was for 1 h thinking that I don't know how to use patch/mock. ;) |
I've just come across this too, so would be great if the patch can be progressed. |
Here's a minimal example so my comment is not totally vacuous:
->
|
Adding to the list of "I just ran into this". The patch submitted by fov seems straightforward enough: what can we do to help shepherd it along? |
Thanks for the pings. I will work on this issue this weekend. Note that 3.4 and 3.5 are in security-fix-only mode now, so I removed them from the versions field. |
Were you able to make any progress on this? Do you need any help? |
@berker.peksag I have converted the patch at https://bugs.python.org/file40470/issue23078.patch and pushed it to a GitHub branch master...tirkarthi:bpo-23078 . I am willing to open a PR attributing to @fov in case you haven't had the time to look into this. I am removing 3.6 since it's security fixes only mode. Thanks |
Please go ahead with the PR. I can't push this one through, but would be On Thu, 17 Jan 2019 at 03:42, Karthikeyan Singaravelan <
|
Thank you! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: