Skip to content

Commit

Permalink
fix: coroutine -> AnyAwaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Jul 30, 2024
1 parent e01ac00 commit 7f30e71
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/timeout_executor/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import anyio
import cloudpickle
from typing_extensions import ParamSpec, Self, TypeVar, override
from typing_extensions import ParamSpec, Self, TypeAlias, TypeVar, override

from timeout_executor.const import SUBPROCESS_COMMAND, TIMEOUT_EXECUTOR_INPUT_FILE
from timeout_executor.logging import logger
Expand All @@ -25,7 +25,7 @@
from timeout_executor.types import Callback, CallbackArgs, ExecutorArgs, ProcessCallback

if TYPE_CHECKING:
from collections.abc import Coroutine, Iterable
from collections.abc import Awaitable, Coroutine, Iterable

from timeout_executor.main import TimeoutExecutor

Expand All @@ -35,6 +35,7 @@
T = TypeVar("T", infer_variance=True)
P2 = ParamSpec("P2")
T2 = TypeVar("T2", infer_variance=True)
AnyAwaitable: TypeAlias = "Awaitable[T] | Coroutine[Any, Any, T]"


class Executor(Callback[P, T], Generic[P, T]):
Expand Down Expand Up @@ -179,7 +180,7 @@ def remove_callback(self, callback: ProcessCallback[P, T]) -> Self:
@overload
def apply_func(
timeout_or_executor: float | TimeoutExecutor,
func: Callable[P2, Coroutine[Any, Any, T2]],
func: Callable[P2, AnyAwaitable[T2]],
*args: P2.args,
**kwargs: P2.kwargs,
) -> AsyncResult[P2, T2]: ...
Expand Down Expand Up @@ -223,7 +224,7 @@ def apply_func(
@overload
async def delay_func(
timeout_or_executor: float | TimeoutExecutor,
func: Callable[P2, Coroutine[Any, Any, T2]],
func: Callable[P2, AnyAwaitable[T2]],
*args: P2.args,
**kwargs: P2.kwargs,
) -> AsyncResult[P2, T2]: ...
Expand Down
20 changes: 6 additions & 14 deletions src/timeout_executor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from contextlib import suppress
from typing import TYPE_CHECKING, Any, Callable, Generic, overload

from typing_extensions import ParamSpec, Self, TypeVar, override
from typing_extensions import ParamSpec, Self, TypeAlias, TypeVar, override

from timeout_executor.executor import apply_func, delay_func
from timeout_executor.types import Callback, ProcessCallback

if TYPE_CHECKING:
from collections.abc import Coroutine, Iterable
from collections.abc import Awaitable, Coroutine, Iterable

from timeout_executor.result import AsyncResult

Expand All @@ -19,6 +19,7 @@
P = ParamSpec("P")
T = TypeVar("T", infer_variance=True)
AnyT = TypeVar("AnyT", infer_variance=True, default=Any)
AnyAwaitable: TypeAlias = "Awaitable[T] | Coroutine[Any, Any, T]"


class TimeoutExecutor(Callback[Any, AnyT], Generic[AnyT]):
Expand All @@ -35,10 +36,7 @@ def timeout(self) -> float:

@overload
def apply(
self,
func: Callable[P, Coroutine[Any, Any, T]],
*args: P.args,
**kwargs: P.kwargs,
self, func: Callable[P, AnyAwaitable[T]], *args: P.args, **kwargs: P.kwargs
) -> AsyncResult[P, T]: ...
@overload
def apply(
Expand All @@ -61,10 +59,7 @@ def apply(

@overload
async def delay(
self,
func: Callable[P, Coroutine[Any, Any, T]],
*args: P.args,
**kwargs: P.kwargs,
self, func: Callable[P, AnyAwaitable[T]], *args: P.args, **kwargs: P.kwargs
) -> AsyncResult[P, T]: ...
@overload
async def delay(
Expand All @@ -87,10 +82,7 @@ async def delay(

@overload
async def apply_async(
self,
func: Callable[P, Coroutine[Any, Any, T]],
*args: P.args,
**kwargs: P.kwargs,
self, func: Callable[P, AnyAwaitable[T]], *args: P.args, **kwargs: P.kwargs
) -> AsyncResult[P, T]: ...
@overload
async def apply_async(
Expand Down
11 changes: 5 additions & 6 deletions src/timeout_executor/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@

import anyio
import cloudpickle
from typing_extensions import ParamSpec, TypeVar
from typing_extensions import ParamSpec, TypeAlias, TypeVar

from timeout_executor.const import TIMEOUT_EXECUTOR_INPUT_FILE
from timeout_executor.serde import dumps_error

if TYPE_CHECKING:
from collections.abc import Coroutine
from collections.abc import Awaitable, Coroutine

__all__ = []

P = ParamSpec("P")
T = TypeVar("T", infer_variance=True)
AnyAwaitable: TypeAlias = "Awaitable[T] | Coroutine[Any, Any, T]"


def run_in_subprocess() -> None:
Expand Down Expand Up @@ -82,14 +83,12 @@ def inner(*args: P.args, **kwargs: P.kwargs) -> T:

def _output_to_file_async(
file: Path | anyio.Path,
) -> Callable[
[Callable[P, Coroutine[Any, Any, T]]], Callable[P, Coroutine[Any, Any, T]]
]:
) -> Callable[[Callable[P, AnyAwaitable[T]]], Callable[P, Coroutine[Any, Any, T]]]:
if isinstance(file, Path):
file = anyio.Path(file)

def wrapper(
func: Callable[P, Coroutine[Any, Any, T]],
func: Callable[P, AnyAwaitable[T]],
) -> Callable[P, Coroutine[Any, Any, T]]:
async def inner(*args: P.args, **kwargs: P.kwargs) -> T:
dump = b""
Expand Down

0 comments on commit 7f30e71

Please sign in to comment.