Skip to content

Commit e553f20

Browse files
authored
bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18924)
Create call objects with awaited arguments instead of using call_args which has only last call value.
1 parent fde44ae commit e553f20

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ async def _execute_mock_call(self, /, *args, **kwargs):
21712171
# This is nearly just like super(), except for special handling
21722172
# of coroutines
21732173

2174-
_call = self.call_args
2174+
_call = _Call((args, kwargs), two=True)
21752175
self.await_count += 1
21762176
self.await_args = _call
21772177
self.await_args_list.append(_call)

Lib/unittest/test/testmock/testasync.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ def inner():
500500
mock.assert_awaited()
501501
self.assertTrue(ran)
502502

503+
async def test_await_args_list_order(self):
504+
async_mock = AsyncMock()
505+
mock2 = async_mock(2)
506+
mock1 = async_mock(1)
507+
await mock1
508+
await mock2
509+
async_mock.assert_has_awaits([call(1), call(2)])
510+
self.assertEqual(async_mock.await_args_list, [call(1), call(2)])
511+
self.assertEqual(async_mock.call_args_list, [call(2), call(1)])
512+
513+
503514
class AsyncMagicMethods(unittest.TestCase):
504515
def test_async_magic_methods_return_async_mocks(self):
505516
m_mock = MagicMock()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Ensure :attr:`unittest.mock.AsyncMock.await_args_list` has call objects in
2+
the order of awaited arguments instead of using
3+
:attr:`unittest.mock.Mock.call_args` which has the last value of the call.
4+
Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)