Skip to content

Commit

Permalink
Use async def instead of the deprecated asyncio.coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
kiendang committed Jan 4, 2023
1 parent 9cc1593 commit 5430779
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion graphql_server/flask/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
GraphiQLOptions,
render_graphiql_sync,
)
from graphql_server.utils import wrap_in_async


class GraphQLView(View):
Expand Down Expand Up @@ -121,7 +122,7 @@ async def get_async_execution_results():
*(
ex
if ex is not None and is_awaitable(ex)
else asyncio.coroutine(lambda: ex)()
else wrap_in_async(lambda: ex)()
for ex in execution_results
)
)
Expand Down
25 changes: 25 additions & 0 deletions graphql_server/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
from typing import Awaitable, Callable, TypeVar

if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
from typing_extensions import ParamSpec


__all__ = ["wrap_in_async"]

P = ParamSpec("P")
R = TypeVar("R")


def wrap_in_async(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:
"""Convert a sync callable (normal def or lambda) to a coroutine (async def).
This is similar to asyncio.coroutine which was deprecated in Python 3.8.
"""

async def f_async(*args: P.args, **kwargs: P.kwargs) -> R:
return f(*args, **kwargs)

return f_async

0 comments on commit 5430779

Please sign in to comment.