mirror of https://github.com/python/cpython.git
bpo-37075: Fix string concatenation in assert_has_awaits error message (GH-13616)
* Fix the implicit string concatenation in `assert_has_awaits` error message. * Use "await" instead of "call" in `assert_awaited_with` error message. https://bugs.python.org/issue37075
This commit is contained in:
parent
744c08a9c7
commit
0ae022c6a4
|
@ -1954,7 +1954,7 @@ The full list of supported magic methods is:
|
||||||
* Container methods: ``__getitem__``, ``__setitem__``, ``__delitem__``,
|
* Container methods: ``__getitem__``, ``__setitem__``, ``__delitem__``,
|
||||||
``__contains__``, ``__len__``, ``__iter__``, ``__reversed__``
|
``__contains__``, ``__len__``, ``__iter__``, ``__reversed__``
|
||||||
and ``__missing__``
|
and ``__missing__``
|
||||||
* Context manager: ``__enter__``, ``__exit__``, ``__aenter`` and ``__aexit__``
|
* Context manager: ``__enter__``, ``__exit__``, ``__aenter__`` and ``__aexit__``
|
||||||
* Unary numeric methods: ``__neg__``, ``__pos__`` and ``__invert__``
|
* Unary numeric methods: ``__neg__``, ``__pos__`` and ``__invert__``
|
||||||
* The numeric methods (including right hand and in-place variants):
|
* The numeric methods (including right hand and in-place variants):
|
||||||
``__add__``, ``__sub__``, ``__mul__``, ``__matmul__``, ``__div__``, ``__truediv__``,
|
``__add__``, ``__sub__``, ``__mul__``, ``__matmul__``, ``__div__``, ``__truediv__``,
|
||||||
|
@ -2036,6 +2036,7 @@ Methods and their defaults:
|
||||||
* ``__len__``: 0
|
* ``__len__``: 0
|
||||||
* ``__iter__``: iter([])
|
* ``__iter__``: iter([])
|
||||||
* ``__exit__``: False
|
* ``__exit__``: False
|
||||||
|
* ``__aexit__``: False
|
||||||
* ``__complex__``: 1j
|
* ``__complex__``: 1j
|
||||||
* ``__float__``: 1.0
|
* ``__float__``: 1.0
|
||||||
* ``__bool__``: True
|
* ``__bool__``: True
|
||||||
|
|
|
@ -791,12 +791,12 @@ def _format_mock_call_signature(self, args, kwargs):
|
||||||
return _format_call_signature(name, args, kwargs)
|
return _format_call_signature(name, args, kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _format_mock_failure_message(self, args, kwargs):
|
def _format_mock_failure_message(self, args, kwargs, action='call'):
|
||||||
message = 'expected call not found.\nExpected: %s\nActual: %s'
|
message = 'expected %s not found.\nExpected: %s\nActual: %s'
|
||||||
expected_string = self._format_mock_call_signature(args, kwargs)
|
expected_string = self._format_mock_call_signature(args, kwargs)
|
||||||
call_args = self.call_args
|
call_args = self.call_args
|
||||||
actual_string = self._format_mock_call_signature(*call_args)
|
actual_string = self._format_mock_call_signature(*call_args)
|
||||||
return message % (expected_string, actual_string)
|
return message % (action, expected_string, actual_string)
|
||||||
|
|
||||||
|
|
||||||
def _call_matcher(self, _call):
|
def _call_matcher(self, _call):
|
||||||
|
@ -2139,7 +2139,7 @@ def assert_awaited_with(_mock_self, *args, **kwargs):
|
||||||
raise AssertionError(f'Expected await: {expected}\nNot awaited')
|
raise AssertionError(f'Expected await: {expected}\nNot awaited')
|
||||||
|
|
||||||
def _error_message():
|
def _error_message():
|
||||||
msg = self._format_mock_failure_message(args, kwargs)
|
msg = self._format_mock_failure_message(args, kwargs, action='await')
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
expected = self._call_matcher((args, kwargs))
|
expected = self._call_matcher((args, kwargs))
|
||||||
|
@ -2193,7 +2193,7 @@ def assert_has_awaits(_mock_self, calls, any_order=False):
|
||||||
if not any_order:
|
if not any_order:
|
||||||
if expected not in all_awaits:
|
if expected not in all_awaits:
|
||||||
raise AssertionError(
|
raise AssertionError(
|
||||||
f'Awaits not found.\nExpected: {_CallList(calls)}\n',
|
f'Awaits not found.\nExpected: {_CallList(calls)}\n'
|
||||||
f'Actual: {self.await_args_list}'
|
f'Actual: {self.await_args_list}'
|
||||||
) from cause
|
) from cause
|
||||||
return
|
return
|
||||||
|
|
|
@ -542,7 +542,8 @@ def test_assert_awaited_once(self):
|
||||||
|
|
||||||
def test_assert_awaited_with(self):
|
def test_assert_awaited_with(self):
|
||||||
asyncio.run(self._runnable_test())
|
asyncio.run(self._runnable_test())
|
||||||
with self.assertRaises(AssertionError):
|
msg = 'expected await not found'
|
||||||
|
with self.assertRaisesRegex(AssertionError, msg):
|
||||||
self.mock.assert_awaited_with('foo')
|
self.mock.assert_awaited_with('foo')
|
||||||
|
|
||||||
asyncio.run(self._runnable_test('foo'))
|
asyncio.run(self._runnable_test('foo'))
|
||||||
|
@ -580,8 +581,9 @@ def test_assert_any_wait(self):
|
||||||
def test_assert_has_awaits_no_order(self):
|
def test_assert_has_awaits_no_order(self):
|
||||||
calls = [call('NormalFoo'), call('baz')]
|
calls = [call('NormalFoo'), call('baz')]
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError) as cm:
|
||||||
self.mock.assert_has_awaits(calls)
|
self.mock.assert_has_awaits(calls)
|
||||||
|
self.assertEqual(len(cm.exception.args), 1)
|
||||||
|
|
||||||
asyncio.run(self._runnable_test('foo'))
|
asyncio.run(self._runnable_test('foo'))
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
|
|
Loading…
Reference in New Issue