Patched async function not registering calls until coroutine is awaited
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci il comportamento usando tests/other_module.py e tests/test_1.py, concentrandoti su patch.object con side_effect e autospec=True. Verifica quando il mock registra la chiamata rispetto alla creazione e all’attesa della coroutine. Il lavoro è completato quando la prima asserzione passa prima di await e il comportamento è coperto da un test CPython appropriato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- testing-qa
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 48/100