Skip to content

Commit e8488eb

Browse files
committed
Use async def instead of the deprecated asyncio.coroutine
1 parent 9cc1593 commit e8488eb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

graphql_server/flask/graphqlview.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
GraphiQLOptions,
2727
render_graphiql_sync,
2828
)
29+
from graphql_server.utils import ensure_async
2930

3031

3132
class GraphQLView(View):
@@ -121,7 +122,7 @@ async def get_async_execution_results():
121122
*(
122123
ex
123124
if ex is not None and is_awaitable(ex)
124-
else asyncio.coroutine(lambda: ex)()
125+
else ensure_async(lambda: ex)()
125126
for ex in execution_results
126127
)
127128
)

graphql_server/utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
from typing import Awaitable, Callable, TypeVar
3+
4+
if sys.version_info >= (3, 10):
5+
from typing import ParamSpec
6+
else:
7+
from typing_extensions import ParamSpec
8+
9+
10+
__all__ = ["ensure_async"]
11+
12+
P = ParamSpec("P")
13+
R = TypeVar("R")
14+
15+
16+
def ensure_async(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:
17+
"""Convert a sync callable (normal def or lambda) to a coroutine (async def).
18+
19+
This is similar to asyncio.coroutine which was deprecated in Python 3.8.
20+
"""
21+
22+
async def f_async(*args: P.args, **kwargs: P.kwargs) -> R:
23+
return f(*args, **kwargs)
24+
25+
return f_async

0 commit comments

Comments
 (0)