-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use async def instead of the deprecated asyncio.coroutine
- Loading branch information
Showing
2 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ = ["ensure_async"] | ||
|
||
P = ParamSpec("P") | ||
R = TypeVar("R") | ||
|
||
|
||
def ensure_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 |