-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-36871: Handle spec errors in assert_has_calls #16005
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
Changes from all commits
c3e7312
6e44f0c
f0ceae7
587eb55
95beaaa
4e9bd60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -931,13 +931,21 @@ def assert_has_calls(self, calls, any_order=False): | |||||
If `any_order` is True then the calls can be in any order, but | ||||||
they must all appear in `mock_calls`.""" | ||||||
expected = [self._call_matcher(c) for c in calls] | ||||||
cause = expected if isinstance(expected, Exception) else None | ||||||
cause = next((e for e in expected if isinstance(e, Exception)), None) | ||||||
all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls) | ||||||
if not any_order: | ||||||
if expected not in all_calls: | ||||||
if cause is None: | ||||||
problem = 'Calls not found.' | ||||||
else: | ||||||
problem = ('Error processing expected calls.\n' | ||||||
'Errors: {}').format( | ||||||
[e if isinstance(e, Exception) else None | ||||||
for e in expected]) | ||||||
raise AssertionError( | ||||||
'Calls not found.\nExpected: %r%s' | ||||||
% (_CallList(calls), self._calls_repr(prefix="Actual")) | ||||||
f'{problem}\n' | ||||||
f'Expected: {_CallList(calls)}\n' | ||||||
f'Actual: {self._calls_repr(prefix="Actual")}' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This causes a difference in error message with Actual line printed twice. Using from unittest.mock import *
def f(): pass
m = Mock(spec=f)
m()
m.assert_has_calls([call(), call('wrong')])
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whooops. thanks! i missed that. :) follow up with a new PR with that little change. looks like we need to do the backport manually and can lump these two changes together for that anyways. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, sorry. I was making this more similar to cleaner logic in assert_has_awaits, and I missed that repetition. |
||||||
) from cause | ||||||
return | ||||||
|
||||||
|
@@ -2214,12 +2222,20 @@ def assert_has_awaits(self, calls, any_order=False): | |||||
they must all appear in :attr:`await_args_list`. | ||||||
""" | ||||||
expected = [self._call_matcher(c) for c in calls] | ||||||
cause = expected if isinstance(expected, Exception) else None | ||||||
cause = next((e for e in expected if isinstance(e, Exception)), None) | ||||||
all_awaits = _CallList(self._call_matcher(c) for c in self.await_args_list) | ||||||
if not any_order: | ||||||
if expected not in all_awaits: | ||||||
if cause is None: | ||||||
problem = 'Awaits not found.' | ||||||
else: | ||||||
problem = ('Error processing expected awaits.\n' | ||||||
'Errors: {}').format( | ||||||
[e if isinstance(e, Exception) else None | ||||||
for e in expected]) | ||||||
raise AssertionError( | ||||||
f'Awaits not found.\nExpected: {_CallList(calls)}\n' | ||||||
f'{problem}\n' | ||||||
f'Expected: {_CallList(calls)}\n' | ||||||
f'Actual: {self.await_args_list}' | ||||||
) from cause | ||||||
return | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Improve error handling for the assert_has_calls and assert_has_awaits methods of | ||
mocks. Fixed a bug where any errors encountered while binding the expected calls | ||
to the mock's spec were silently swallowed, leading to misleading error output. |
Uh oh!
There was an error while loading. Please reload this page.