python / python/cpython

async callable not awaited in side_effect for AsyncMock

Abierto
#156,460 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

In the AsyncMockMixin._execute_mock_call, the following test is done:

        if effect is not None:
            if _is_exception(effect):
                raise effect
            elif not _callable(effect):
                try:
                    result = next(effect)
                except StopIteration:
                    # It is impossible to propagate a StopIteration
                    # through coroutines because of PEP 479
                    raise StopAsyncIteration
                if _is_exception(result):
                    raise result
            elif iscoroutinefunction(effect):
                result = await effect(*args, **kwargs)
            else:
                result = effect(*args, **kwargs)

However elif iscoroutinefunction(effect) will return False for an awaitable that was defined this way:

    class AsyncCallable:
        async def __call__(self, *args, **kwargs):
            # await stuff
            pass

Note that __call__ is awaitable here but not detected as such.

iscoroutinefunction(effect.__call__) would return True in such a case however, so the fix might be as simple as:

            elif iscoroutinefunction(effect) or iscoroutinefunction(getattr(effect, '__call__', None)):

In the meantime, the workaround is to add markcoroutinefunction(self) to the class.

    class AsyncCallable:
        def __init__(self, *args, **kwargs):
            # init code
            markcoroutinefunction(self)  # workaround for iscoroutinefunction check in side_effect to return True

        ...
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-156465

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en Lib/unittest/mock.py, en AsyncMockMixin._execute_mock_call, y revisa el PR vinculado gh-156465. Confirma el comportamiento de un objeto cuya call asíncrona se usa como AsyncMock.side_effect y añade o actualiza la cobertura de regresión para que se haga await correctamente de ese objeto invocable.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
testing-qa
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.