Skip to content

Commit f6bdac1

Browse files
bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18927)
Create call objects with awaited arguments instead of using call_args which has only last call value. (cherry picked from commit e553f20) Co-authored-by: Karthikeyan Singaravelan <[email protected]>
1 parent d3af92e commit f6bdac1

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
@@ -2136,7 +2136,7 @@ async def _execute_mock_call(self, /, *args, **kwargs):
21362136
# This is nearly just like super(), except for sepcial handling
21372137
# of coroutines
21382138

2139-
_call = self.call_args
2139+
_call = _Call((args, kwargs), two=True)
21402140
self.await_count += 1
21412141
self.await_args = _call
21422142
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
@@ -496,6 +496,17 @@ def inner():
496496
mock.assert_awaited()
497497
self.assertTrue(ran)
498498

499+
async def test_await_args_list_order(self):
500+
async_mock = AsyncMock()
501+
mock2 = async_mock(2)
502+
mock1 = async_mock(1)
503+
await mock1
504+
await mock2
505+
async_mock.assert_has_awaits([call(1), call(2)])
506+
self.assertEqual(async_mock.await_args_list, [call(1), call(2)])
507+
self.assertEqual(async_mock.call_args_list, [call(2), call(1)])
508+
509+
499510
class AsyncMagicMethods(unittest.TestCase):
500511
def test_async_magic_methods_return_async_mocks(self):
501512
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)