Python Glossary Русский

Python 3.5+typesterm

types.CoroutineType

generators and coroutines

The type of the coroutine object returned by calling an async def function. It is not the result yet: the coroutine must be awaited or passed to asyncio.run.

Syntax

isinstance(obj, types.CoroutineType)

Calling an async function without await runs nothing — it only creates a CoroutineType; an abandoned one triggers RuntimeWarning "coroutine ... was never awaited" when collected. isinstance(x, types.CoroutineType) is narrow: a generator-based coroutine from @types.coroutine and any object with __await__ (asyncio.Future, Task) do not match it. To accept anything awaitable, use inspect.isawaitable or collections.abc.Awaitable.

Examples

import asyncio
import types
async def fetch():
    return 42
c = fetch()
print(isinstance(c, types.CoroutineType))  # → True
print(asyncio.run(c))  # → 42

See also

Open in the full glossarydocs.python.orgReport an inaccuracy