`inspect.iscoroutinefunction()` does not detect marked partial objects
Nadie ha tomado este issue todavía.
- 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:
inspect.markcoroutinefunction() is applied directly to the passed function, except for methods (which is correct behavior). However, inspect.iscoroutinefunction() checks the passed function only after unwrapping (a function that is not wrapped in either functools.partial or (currently, functools.partialmethod_has_coroutine_mark() does not handle these objects; why?)), and thus cannot detect the marker that is not at the end of the unwrapping chain, which results in false negative in cases where a functools.partial/ object is marked.functools.partialmethod
>>> from functools import partial
>>> from inspect import iscoroutinefunction, markcoroutinefunction
>>> async def wedonotlikesnakecase():
... return "the_funniest_joke_in_the_world"
>>> def manufacturer_of_jokes(somefunc):
... global manufacturer_of_jokes
... del manufacturer_of_jokes
... return somefunc()
>>> joke = partial(manufacturer_of_jokes, wedonotlikesnakecase)
>>> joke = markcoroutinefunction(joke)
>>> iscoroutinefunction(joke)
False
I discovered this problem while theoretically considering backporting inspect.iscoroutinefunction() to older versions of Python. The problem has not yet been caused by any use case, so it is okay if the issue is closed due to lack of demand. But just in case, I am attaching part of how iscoroutinefunction() is implemented in my code.
def iscoroutinefunction(obj):
marker_name, marker_value = _get_coroutinefunction_marker()
while True: # unwrap & check
if ismethod(obj):
obj = obj.__func__
continue
if marker_value is not MISSING:
if getattr(obj, marker_name, MISSING) is marker_value:
return True
if isinstance(obj, partial):
obj = obj.func
continue
impl = getattr(obj, _partialmethod_attribute_name, MISSING)
if isinstance(impl, partialmethod):
obj = impl.func
continue
# unlike its namesake in the `inspect` module, it does not unwrap
return _has_code_flag(obj, CO_COROUTINE)
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Linux
Linked PRs
- gh-142503
- gh-142505
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza con inspect.iscoroutinefunction(), inspect.markcoroutinefunction() y _has_coroutine_mark(), usando el reproductor del issue para confirmar el falso negativo con un functools.partial marcado. Revisa los PRs vinculados gh-142503 y gh-142505 antes de empezar; se considera terminado cuando el caso notificado del objeto partial se detecta correctamente sin provocar regresiones en el comportamiento existente de métodos y desempaquetado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100