Skip to content

Commit

Permalink
Merge pull request #208 from DABND19/fix/coroutinefunctions
Browse files Browse the repository at this point in the history
fix: Return coroutines instead of awaitables from functions.
  • Loading branch information
mosquito committed May 14, 2024
2 parents 3d38ade + e7779b9 commit 9d80cb7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
15 changes: 10 additions & 5 deletions aiomisc/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from inspect import Parameter
from typing import (
Any,
Awaitable,
Callable,
Coroutine,
Generic,
Iterable,
List,
Expand Down Expand Up @@ -246,7 +246,7 @@ def __init__(

def aggregate(
leeway_ms: float, max_count: Optional[int] = None
) -> Callable[[AggregateFunc[V, R]], Callable[[V], Awaitable[R]]]:
) -> Callable[[AggregateFunc[V, R]], Callable[[V], Coroutine[Any, Any, R]]]:
"""
Parametric decorator that aggregates multiple
(but no more than ``max_count`` defaulting to ``None``) single-argument
Expand Down Expand Up @@ -275,7 +275,9 @@ def aggregate(
:return:
"""
def decorator(func: AggregateFunc[V, R]) -> Callable[[V], Awaitable[R]]:
def decorator(
func: AggregateFunc[V, R]
) -> Callable[[V], Coroutine[Any, Any, R]]:
aggregator = Aggregator(
func, max_count=max_count, leeway_ms=leeway_ms,
)
Expand All @@ -285,7 +287,10 @@ def decorator(func: AggregateFunc[V, R]) -> Callable[[V], Awaitable[R]]:

def aggregate_async(
leeway_ms: float, max_count: Optional[int] = None,
) -> Callable[[AggregateAsyncFunc[V, R]], Callable[[V], Awaitable[R]]]:
) -> Callable[
[AggregateAsyncFunc[V, R]],
Callable[[V], Coroutine[Any, Any, R]]
]:
"""
Same as ``aggregate``, but with ``func`` arguments of type ``Arg``
containing ``value`` and ``future`` attributes instead. In this setting
Expand All @@ -298,7 +303,7 @@ def aggregate_async(
"""
def decorator(
func: AggregateAsyncFunc[V, R]
) -> Callable[[V], Awaitable[R]]:
) -> Callable[[V], Coroutine[Any, Any, R]]:
aggregator = AggregatorAsync(
func, max_count=max_count, leeway_ms=leeway_ms,
)
Expand Down
16 changes: 12 additions & 4 deletions aiomisc/backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from functools import wraps
from typing import (
Any, Awaitable, Callable, Optional, Tuple, Type, TypeVar, Union,
Any, Callable, Coroutine, Optional, Tuple, Type, TypeVar, Union,
)

from .counters import Statistic
Expand Down Expand Up @@ -42,7 +42,10 @@ def asyncbackoff(
giveup: Optional[Callable[[Exception], bool]] = None,
statistic_name: Optional[str] = None,
statistic_class: Type[BackoffStatistic] = BackoffStatistic,
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
) -> Callable[
[Callable[P, Coroutine[Any, Any, T]]],
Callable[P, Coroutine[Any, Any, T]],
]:
"""
Patametric decorator that ensures that ``attempt_timeout`` and
``deadline`` time limits are met by decorated function.
Expand Down Expand Up @@ -85,7 +88,9 @@ def asyncbackoff(
exceptions = tuple(exceptions) or ()
exceptions += asyncio.TimeoutError,

def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
def decorator(
func: Callable[P, Coroutine[Any, Any, T]]
) -> Callable[P, Coroutine[Any, Any, T]]:
if attempt_timeout is not None:
func = timeout(attempt_timeout)(func)

Expand Down Expand Up @@ -145,7 +150,10 @@ def asyncretry(
pause: Number = 0,
giveup: Optional[Callable[[Exception], bool]] = None,
statistic_name: Optional[str] = None,
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
) -> Callable[
[Callable[P, Coroutine[Any, Any, T]]],
Callable[P, Coroutine[Any, Any, T]],
]:
"""
Shortcut of ``asyncbackoff(None, None, 0, Exception)``.
Expand Down
11 changes: 7 additions & 4 deletions aiomisc/timeout.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import sys
from functools import wraps
from typing import Awaitable, Callable, TypeVar, Union
from typing import Any, Callable, Coroutine, TypeVar, Union


if sys.version_info >= (3, 10):
Expand All @@ -17,10 +17,13 @@

def timeout(
value: Number
) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]:
) -> Callable[
[Callable[P, Coroutine[Any, Any, T]]],
Callable[P, Coroutine[Any, Any, T]],
]:
def decorator(
func: Callable[P, Awaitable[T]],
) -> Callable[P, Awaitable[T]]:
func: Callable[P, Coroutine[Any, Any, T]],
) -> Callable[P, Coroutine[Any, Any, T]]:
if not asyncio.iscoroutinefunction(func):
raise TypeError("Function is not a coroutine function")

Expand Down

0 comments on commit 9d80cb7

Please sign in to comment.