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: Double patching instance method: AttributeError: Mock object has no attribute '__name__' #70891
Comments
Originally from testing-cabal/mock#350 ## Example from unittest import mock
class C(object):
def f(self):
pass
c = C()
with mock.patch.object(c, 'f', autospec=True):
with mock.patch.object(c, 'f', autospec=True):
pass ## Python3.3
## Python3.4 / 3.5 / 3.6 (From gitbhub.com/python/cpython@fa3fc6d7)
|
The root cause seems to be that autospecced functions return a function object (not a Mock instance) which a '.mock' attribute which is a MagicMock ( assigned here: Line 198 in ae775ab
I took a first stab at a patch (attached) |
Here's an improved patch which:
I'm not entirely happy with the approach -- open to suggestions :) |
Seems I've named the patchfile incorrectly -- Hopefully this is correct this time? |
Thanks @asottile for the patch. I think the original AttributeError is resolved with bpo-28919 where they were silenced. It seems similar to bpo-32153 though the exception occurs from mock instead of partial object as in bpo-32153. The fix was applied to 3.7+ and hence 3.6 was not fixed which enters security fix only mode shortly. The attached tests also pass on master and I think it will it be a good unittest addition to Lib/unittest/test/testmock/testwith.py (similar to bpo-32153) that has a couple of nested with statements for the same attribute. Adding cjw296 to the list. |
I've opened a PR with the test included: #11085 |
Before we get too far: what's the use case for this double patching? |
to be honest, I don't recall exactly given it's been 2 and a half years since the original report with no activity. if I recall correctly, this was encountered while upgrading the I want to say the reason this was hard to "fix" properly was due to some blanket patches being applied in a base test case and then other test cases re-patching those methods to add more specific behaviour. This worked fine in python2.7 and all the way until python3.3 but then was broken by changes in python3.4 Fortunately, they've been fixed in python3.7. I guess I've been encouraged to write a patch with a test so it does not regress in the future |
Ah, yeah, I can see the blanket patch and a more local patch in a monorepo being a thing, cool, let's have a look! |
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: