Skip to content

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

@GabrielSalla

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

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

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