Files
zenml/venv/lib/python3.9/site-packages/graphql/pyutils/is_awaitable.py
Christian Mantha 2ca0b9ef7c star
2026-03-02 19:10:52 -05:00

25 lines
798 B
Python

import inspect
from typing import Any
from types import CoroutineType, GeneratorType
__all__ = ["is_awaitable"]
CO_ITERABLE_COROUTINE = inspect.CO_ITERABLE_COROUTINE
def is_awaitable(value: Any) -> bool:
"""Return true if object can be passed to an ``await`` expression.
Instead of testing if the object is an instance of abc.Awaitable, it checks
the existence of an `__await__` attribute. This is much faster.
"""
return (
# check for coroutine objects
isinstance(value, CoroutineType)
# check for old-style generator based coroutine objects
or isinstance(value, GeneratorType)
and bool(value.gi_code.co_flags & CO_ITERABLE_COROUTINE)
# check for other awaitables (e.g. futures)
or hasattr(value, "__await__")
)