[Python-checkins] bpo-39915: Ensure await_args_list is updated according to the order in which coroutines were awaited (GH-18924)

Karthikeyan Singaravelan webhook-mailer at python.org
Wed Mar 11 11:06:19 EDT 2020


https://github.com/python/cpython/commit/e553f204bf0e39b1d701a364bc71b286acb9433f
commit: e553f204bf0e39b1d701a364bc71b286acb9433f
branch: master
author: Karthikeyan Singaravelan <tir.karthi at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-03-11T15:06:12Z
summary:

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.

files:
A Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testasync.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 9e692981a229b..20daf724c1218 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2171,7 +2171,7 @@ def __init__(self, /, *args, **kwargs):
         # This is nearly just like super(), except for special 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 bf936220ccaf2..690ca4f55f9e3 100644
--- a/Lib/unittest/test/testmock/testasync.py
+++ b/Lib/unittest/test/testmock/testasync.py
@@ -500,6 +500,17 @@ def inner():
         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()
diff --git a/Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst b/Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst
new file mode 100644
index 0000000000000..2c369474c2df8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-10-19-38-47.bpo-39915.CjPeiY.rst
@@ -0,0 +1,4 @@
+Ensure :attr:`unittest.mock.AsyncMock.await_args_list` has call objects in
+the order of awaited arguments instead of using
+:attr:`unittest.mock.Mock.call_args` which has the last value of the call.
+Patch by Karthikeyan Singaravelan.



More information about the Python-checkins mailing list