async callable not awaited in side_effect for AsyncMock
未關閉
還沒有人認領這個 Issue。
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
In the AsyncMockMixin._execute_mock_call, the following test is done:
if effect is not None:
if _is_exception(effect):
raise effect
elif not _callable(effect):
try:
result = next(effect)
except StopIteration:
# It is impossible to propagate a StopIteration
# through coroutines because of PEP 479
raise StopAsyncIteration
if _is_exception(result):
raise result
elif iscoroutinefunction(effect):
result = await effect(*args, **kwargs)
else:
result = effect(*args, **kwargs)
However elif iscoroutinefunction(effect) will return False for an awaitable that was defined this way:
class AsyncCallable:
async def __call__(self, *args, **kwargs):
# await stuff
pass
Note that __call__ is awaitable here but not detected as such.
iscoroutinefunction(effect.__call__) would return True in such a case however, so the fix might be as simple as:
elif iscoroutinefunction(effect) or iscoroutinefunction(getattr(effect, '__call__', None)):
In the meantime, the workaround is to add markcoroutinefunction(self) to the class.
class AsyncCallable:
def __init__(self, *args, **kwargs):
# init code
markcoroutinefunction(self) # workaround for iscoroutinefunction check in side_effect to return True
...
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-156465
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Lib/unittest/mock.py 中的 AsyncMockMixin._execute_mock_call 開始,檢視相關的 PR gh-156465。確認將某個物件的非同步 call 方法用作 AsyncMock.side_effect 時的行為,然後新增或更新回歸測試涵蓋範圍,確保該可呼叫物件被正確 await。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- testing-qa
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100