You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't call mocked methods after create_autospec() and seal() #91803
The code below raises the error below. If I'm understanding the documentation for mock.seal correctly, it's supposed to prevent the creation of new attributes on the sealed mock, not prevent calls to existing methods, right?
Traceback (most recent call last):
File "/tmp/tmp.8toUu9Dy3T/foo.py", line 7, in <module>
foo.foo()
File "/usr/lib/python3.9/unittest/mock.py", line 1092, in __call__
return self._mock_call(*args, **kwargs)
File "/usr/lib/python3.9/unittest/mock.py", line 1096, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/usr/lib/python3.9/unittest/mock.py", line 1168, in _execute_mock_call
return self.return_value
File "/usr/lib/python3.9/unittest/mock.py", line 630, in __getattr__
raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'return_value'
Your environment
CPython versions tested on: 3.9.9
Operating system and architecture: Debian testing / amd64
Activity
eugenetriguba commentedon Apr 24, 2022
If you set the return value beforehand or make a call to that method, you can.
This may be related to how
mock.create_autospecis creating the mock. For instance:I'll look into it a little more
akulakov commentedon May 3, 2022
I've added a draft PR #92213 , but I need to work on it a bit more.
akulakov commentedon May 3, 2022
I think the PR may be good, if we consider Mock return value of methods as part of the autospec. That's what such methods return if there is no
seal.However I haven't used
seal()before and I haven't usedautospecin any advanced way, so I hope this can be looked at by an expert inmockmodule.gh-91803: Mock - fix error when using autospec methods with seal (#92213
gh-91803: Mock - fix error when using autospec methods with seal (#92…
gh-91803: Mock - fix error when using autospec methods with seal (#92…