[issue39915] await_args_list in AsyncMock always refers to the last awaited call object

Karthikeyan Singaravelan report at bugs.python.org
Tue Mar 10 05:20:14 EDT 2020


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

Thanks for the report. At [0] self.call_args is used. This value is always the last call. So when the object is awaited it will be always the last call object and gets appended. The id of the call objects remain the same. The fix would be to construct the call object with args and kwargs in asynchronous definition of _execute_mock_call like [1]. This would need tests too. It seems there is only test for one await call and assertion over await_args_list. Having different args and kwargs with multiple await to assert await_args_list would help here. In synchronous case the call object is eagerly appended so it won't be an issue.

[0] https://github.com/python/cpython/blob/8510f430781118d9b603c3a2f06945d6ebc5fe42/Lib/unittest/mock.py#L2174
[1] https://github.com/python/cpython/blob/8510f430781118d9b603c3a2f06945d6ebc5fe42/Lib/unittest/mock.py#L1106

A potential patch would be as below : 

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 9e692981a2..20daf724c1 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2171,7 +2171,7 @@ class AsyncMockMixin(Base):
         # 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)

----------
nosy: +cjw296, mariocj89, michael.foord
title: AsyncMock doesn't work with asyncio.gather -> await_args_list in AsyncMock always refers to the last awaited call object

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39915>
_______________________________________


More information about the Python-bugs-list mailing list