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 e553f204bf0e39b1d701a364bc71b286acb9433f)
Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 204b3e7..a8f74a9 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2136,7 +2136,7 @@
# This is nearly just like super(), except for sepcial handling
# of coroutines
- _call = self.call_args
+ _call = _Call((args, kwargs), two=True)
self.await_count += 1
self.await_args = _call
self.await_args_list.append(_call)
diff --git a/Lib/unittest/test/testmock/testasync.py b/Lib/unittest/test/testmock/testasync.py
index e68022a..e84c66c 100644
--- a/Lib/unittest/test/testmock/testasync.py
+++ b/Lib/unittest/test/testmock/testasync.py
@@ -496,6 +496,17 @@
mock.assert_awaited()
self.assertTrue(ran)
+ async def test_await_args_list_order(self):
+ async_mock = AsyncMock()
+ mock2 = async_mock(2)
+ mock1 = async_mock(1)
+ await mock1
+ await mock2
+ async_mock.assert_has_awaits([call(1), call(2)])
+ self.assertEqual(async_mock.await_args_list, [call(1), call(2)])
+ self.assertEqual(async_mock.call_args_list, [call(2), call(1)])
+
+
class AsyncMagicMethods(unittest.TestCase):
def test_async_magic_methods_return_async_mocks(self):
m_mock = MagicMock()