Skip to content

Commit

Permalink
Add wrap_async helper
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ authored Nov 19, 2024
1 parent 25b048c commit 846166c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/haiway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
throttle,
timeout,
traced,
wrap_async,
)
from haiway.state import State
from haiway.types import (
Expand Down Expand Up @@ -76,4 +77,5 @@
"timeout",
"traced",
"when_missing",
"wrap_async",
]
3 changes: 2 additions & 1 deletion src/haiway/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from haiway.helpers.asynchrony import asynchronous
from haiway.helpers.asynchrony import asynchronous, wrap_async
from haiway.helpers.caching import cache
from haiway.helpers.retries import retry
from haiway.helpers.throttling import throttle
Expand All @@ -14,4 +14,5 @@
"throttle",
"timeout",
"traced",
"wrap_async",
]
17 changes: 17 additions & 0 deletions src/haiway/helpers/asynchrony.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@

__all__ = [
"asynchronous",
"wrap_async",
]


def wrap_async[**Args, Result](
function: Callable[Args, Coroutine[None, None, Result]] | Callable[Args, Result],
/,
) -> Callable[Args, Coroutine[None, None, Result]]:
if iscoroutinefunction(function):
return function

else:

async def async_function(*args: Args.args, **kwargs: Args.kwargs) -> Result:
return cast(Callable[Args, Result], function)(*args, **kwargs)

_mimic_async(function, within=async_function)
return async_function


@overload
def asynchronous[**Args, Result]() -> (
Callable[
Expand Down

0 comments on commit 846166c

Please sign in to comment.