1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
import asyncio
import sys
from inspect import isawaitable
from graphql.pyutils import is_awaitable
from pytest import mark
def describe_is_awaitable():
def declines_the_none_value():
assert not isawaitable(None)
assert not is_awaitable(None)
def declines_a_boolean_value():
assert not isawaitable(True)
assert not is_awaitable(True)
def declines_an_int_value():
assert not is_awaitable(42)
def declines_a_string_value():
assert not isawaitable("some_string")
assert not is_awaitable("some_string")
def declines_a_dict_value():
assert not isawaitable({})
assert not is_awaitable({})
def declines_an_object_instance():
assert not isawaitable(object())
assert not is_awaitable(object())
def declines_the_type_class():
assert not isawaitable(type)
assert not is_awaitable(type)
def declines_a_lambda_function():
assert not isawaitable(lambda: True) # pragma: no cover
assert not is_awaitable(lambda: True) # pragma: no cover
def declines_a_normal_function():
def some_function():
return True
assert not isawaitable(some_function())
assert not is_awaitable(some_function)
def declines_a_normal_generator_function():
def some_generator():
yield True # pragma: no cover
assert not isawaitable(some_generator)
assert not is_awaitable(some_generator)
def declines_a_normal_generator_object():
def some_generator():
yield True # pragma: no cover
assert not isawaitable(some_generator())
assert not is_awaitable(some_generator())
def declines_a_coroutine_function():
async def some_coroutine():
return True # pragma: no cover
assert not isawaitable(some_coroutine)
assert not is_awaitable(some_coroutine)
@mark.asyncio
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
async def recognizes_a_coroutine_object():
async def some_coroutine():
return False # pragma: no cover
assert isawaitable(some_coroutine())
assert is_awaitable(some_coroutine())
@mark.filterwarnings("ignore::Warning") # Deprecation and Runtime
@mark.skipif(
sys.version_info >= (3, 11),
reason="Generator-based coroutines not supported after Python 3.11",
)
def recognizes_an_old_style_coroutine(): # pragma: no cover
@asyncio.coroutine
def some_old_style_coroutine():
yield False # pragma: no cover
assert is_awaitable(some_old_style_coroutine())
assert is_awaitable(some_old_style_coroutine())
@mark.asyncio
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
async def recognizes_a_future_object():
async def some_coroutine():
return False # pragma: no cover
some_future = asyncio.ensure_future(some_coroutine())
assert is_awaitable(some_future)
assert is_awaitable(some_future)
@mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
def declines_an_async_generator():
async def some_async_generator():
yield True # pragma: no cover
assert not isawaitable(some_async_generator())
assert not is_awaitable(some_async_generator())
|