Python Glossary Русский

Python 3.12+inspectfunction

inspect.markcoroutinefunction()

coroutines and generators

A decorator that marks a plain function returning a coroutine so that iscoroutinefunction() reports True for it.

Syntax

@inspect.markcoroutinefunction

The mark is a promise, not a conversion: the function stays synchronous and returns whatever it returned, so marking a function that does not return a coroutine misleads the framework. You get back the same object with a marker attribute; for built-ins and other objects without a __dict__ that is AttributeError. It is for APIs that pick their behaviour via iscoroutinefunction (Django, for example); where possible just write async def.

Examples

import asyncio, inspect
async def fetch():
    return 'data'
@inspect.markcoroutinefunction
def fetch_cached():
    return fetch()  # обычная функция, отдающая корутину
print(inspect.iscoroutinefunction(fetch_cached))  # → True
print(asyncio.run(fetch_cached()))  # → data

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy