Skip to content

Patched async function not registering calls until coroutine is awaited #137594

Description

@GabrielSalla

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

asyncdefa(value):
returnvalue*2

File tests/test_1.py

fromunittest.mockimportpatchimportpytestimportother_modulepytestmark=pytest.mark.asyncio(loop_scope="session")
defb(value):
returnother_module.a(value)
asyncdeftest_1():
withpatch.object(other_module, "a", side_effect=other_module.a, autospec=True) asa_mock:
result=b(10)
assertnota_mock.called# This should be True because 'a' was called, but it's Falseawaitresultasserta_mock.called# Now it's True

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions