gh-76273: Fix autospec mocking of instance and class methods - #146526
gh-76273: Fix autospec mocking of instance and class methods#146526stephenfin wants to merge 1 commit into
Conversation
Currently, when patching instance / class methods with autospec, their
self / cls arguments are not consumed, causing call asserts to fail
(they expect an instance / class reference as the first argument).
Example:
from unittest import mock
class Something(object):
def foo(self, a, b, c, d):
pass
with mock.patch.object(Something, 'foo', autospec=True):
s = Something()
s.foo()
Fix this by skipping the first argument when presented with a method.
Based on python#4476
Signed-off-by: Stephen Finucane <stephen@that.guru>
Co-authored-by: Claudiu Belu <cbelu@cloudbasesolutions.com>
|
Hi! Nice to see some work on this. Btw, we generally avoid force pushes. |
Understood. I've got a small bit more work to do this on to resolve the |
Yes, all PRs are squashed on merge.
I see! It's in https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push. Maybe we can make it more visible elsewhere, what do you think? Let me know what you had read before you created the PR, maybe we can add it there :-) Thanks for saying that you didn't see that note. |
|
Just noting that I do plan to get back to this. I just need to find time to do so.
Ack, thanks 👌
To be fair, I just looked the Makefile and |
|
This PR is stale because it has been open for 30 days with no activity. |
|
as an aside, i really like using autospec in general but there is a related edge case to what stephen is tryign to fix here that has bit me when backporting chagnes to branches that still support python 3.9 on 3.9 i had to do that hack to make functions that had multiple layers of decorators actully work properly with autospec in a version independent way. so it would be good to test with a function that has 2+ levels fo decortors that internally use functools.wraps to ensure that we do not regress the 3.10 behvior adn that the self/cls parmater are properly handeled |
Currently, when patching instance / class methods with
autospec, theirself/clsarguments are not consumed, causing call asserts to fail (they expect an instance / class reference as the first argument).Example:
Fix this by skipping the first argument when presented with a method.
Based on #4476 with the changes to the
Mocksignature stripped out and left for a separate, follow-up PR.