python / python/cpython

Patched async function not registering calls until coroutine is awaited

未關閉
#137,594 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

When using autospec=True the patched async function only has calls registered after the coroutine is awaited. This behavior started happening after I upgraded from 3.12 to 3.13.6.

Code to reproduce

File tests/other_module.py

async def a(value):
    return value * 2

File tests/test_1.py

from unittest.mock import patch

import pytest

import other_module

pytestmark = pytest.mark.asyncio(loop_scope="session")

def b(value):
    return other_module.a(value)


async def test_1():
    with patch.object(other_module, "a", side_effect=other_module.a, autospec=True) as a_mock:
        result = b(10)

        assert not a_mock.called  # This should be True because 'a' was called, but it's False

        await result
        assert a_mock.called  # Now it's True
CPython versions tested on:

3.13

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用 tests/other_module.py 和 tests/test_1.py 重現該行為,重點關注帶有 side_effect 和 autospec=True 的 patch.object。檢查 mock 相對於 coroutine 建立和 await 的時機何時記錄呼叫。完成的標準是:第一個 assertion 在 await 之前通過,且該行為由適當的 CPython 測試涵蓋。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing-qa
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。