pytest-mock icon indicating copy to clipboard operation
pytest-mock copied to clipboard

Spy with async function not registering calls until coroutine is awaited

Open GabrielSalla opened this issue 4 months ago • 3 comments

Started happening after I upgraded to Python version 3.13. Pytest mock version 3.14.1

AsyncMock is only registering calls after the coroutine is awaited, when it should do it when the function is called.

Code to reproduce

File tests/other_module.py

async def a(value):
    return value * 2

File tests/test_1.py

from unittest.mock import AsyncMock

import pytest

import other_module

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


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


async def test_1(mocker):
    a_spy: AsyncMock = mocker.spy(other_module, "a")
    result = b(10)

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

    await result
    assert a_spy.called  # Now it's True

Expected result: a_spy.called should return True even if the coroutine was not awaited.

I could not reproduce the behavior using unittest's patch.

    async def test_1():
        with patch("other_module.a") as a_spy:
            result = b(10)
    
>           assert not a_spy.called  # This should be True because 'a' was called, but it's False
            ^^^^^^^^^^^^^^^^^^^^^^^
E           AssertionError: assert not True
E            +  where True = <AsyncMock name='a' id='139651671861504'>.called

tests/test_1.py:18: AssertionError

GabrielSalla avatar Aug 08 '25 21:08 GabrielSalla

Thanks @GabrielSalla for the report!

I'm short on time to investigate this now, but here is the relevant code if somebody wants to investigate:

https://github.com/pytest-dev/pytest-mock/blob/ba83a70ff9c15c0b8534e7f1a7c6ba5321aea046/src/pytest_mock/plugin.py#L161-L209

nicoddemus avatar Aug 09 '25 12:08 nicoddemus

I'll take a look at it and see if I can find out what's happening

GabrielSalla avatar Aug 09 '25 16:08 GabrielSalla

Digging with the debugger, I was able to narrow down the problem to the autospec parameter.

Using unittest's patch.object with autospec=True (which is the same value when using spy) I'm able to reproduce the same behavior. I'll dig deeper to understand this parameter.

Edit: I think it's a Python bug, not pytest-mock's.

Edit 2: I've opened a bug on cpython https://github.com/python/cpython/issues/137594

GabrielSalla avatar Aug 09 '25 17:08 GabrielSalla