Skip to content

Commit

Permalink
Remove Coro type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomperez98 committed Dec 17, 2024
1 parent ced0f4d commit 2dbec99
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
14 changes: 7 additions & 7 deletions src/resonate/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from resonate.time import now

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

from resonate.dependencies import Dependencies
from resonate.typing import Coro, DurableCoro, DurableFn
from resonate.typing import DurableCoro, DurableFn, Yieldable

P = ParamSpec("P")
T = TypeVar("T")
Expand Down Expand Up @@ -58,7 +58,7 @@ def rfc(self, func: str, /, *args: Any, **kwargs: Any) -> RFC: ... # noqa: ANN4
@overload
def rfc(
self,
func: Callable[Concatenate[Context, P], Coro[Any]],
func: Callable[Concatenate[Context, P], Generator[Yieldable, Any, Any]],
/,
*args: P.args,
**kwargs: P.kwargs,
Expand Down Expand Up @@ -108,7 +108,7 @@ def rfi(self, func: str, /, *args: Any, **kwargs: Any) -> RFI: ... # noqa: ANN4
@overload
def rfi(
self,
func: Callable[Concatenate[Context, P], Coro[Any]],
func: Callable[Concatenate[Context, P], Generator[Yieldable, Any, Any]],
/,
*args: P.args,
**kwargs: P.kwargs,
Expand Down Expand Up @@ -147,7 +147,7 @@ def lfi(
@overload
def lfi(
self,
func: Callable[Concatenate[Context, P], Coro[Any]],
func: Callable[Concatenate[Context, P], Generator[Yieldable, Any, Any]],
/,
*args: P.args,
**kwargs: P.kwargs,
Expand Down Expand Up @@ -191,7 +191,7 @@ def lfc(
@overload
def lfc(
self,
func: Callable[Concatenate[Context, P], Coro[Any]],
func: Callable[Concatenate[Context, P], Generator[Yieldable, Any, Any]],
/,
*args: P.args,
**kwargs: P.kwargs,
Expand Down Expand Up @@ -230,7 +230,7 @@ def lfc(
def deferred(
self,
id: str,
coro: Callable[Concatenate[Context, P], Coro[Any]],
coro: Callable[Concatenate[Context, P], Generator[Yieldable, Any, Any]],
/,
*args: P.args,
**kwargs: P.kwargs,
Expand Down
6 changes: 4 additions & 2 deletions src/resonate/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
from resonate.result import Err, Ok, Result

if TYPE_CHECKING:
from collections.abc import Generator

from resonate.record import Handle, Record
from resonate.scheduler.traits import IScheduler
from resonate.typing import Coro, DurableCoro, DurableFn, Yieldable
from resonate.typing import DurableCoro, DurableFn, Yieldable

T = TypeVar("T")
P = ParamSpec("P")
Expand All @@ -35,7 +37,7 @@ class FinalValue(Generic[T]):


class ResonateCoro(Generic[T]):
def __init__(self, record: Record[T], coro: Coro[T]) -> None:
def __init__(self, record: Record[T], coro: Generator[Yieldable, Any, T]) -> None:
self.id = record.id
self._coro = coro
self._coro_active: bool = True
Expand Down
14 changes: 7 additions & 7 deletions src/resonate/resonate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
from resonate.task_sources.traits import ITaskSource

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

from resonate import retry_policy
from resonate.context import Context
from resonate.record import Handle
from resonate.scheduler.traits import IScheduler
from resonate.stores.local import LocalStore
from resonate.task_sources.traits import ITaskSource
from resonate.typing import Coro, DurableCoro, DurableFn
from resonate.typing import DurableCoro, DurableFn, Yieldable

P = ParamSpec("P")
T = TypeVar("T")
Expand Down Expand Up @@ -63,7 +63,7 @@ def register(
self,
func: Callable[
Concatenate[Context, P],
Coro[T],
Generator[Yieldable, Any, T],
],
name: str | None = None,
version: int = 1,
Expand Down Expand Up @@ -106,7 +106,7 @@ def run(
id: str,
func: Callable[
Concatenate[Context, P],
Coro[T],
Generator[Yieldable, Any, T],
]
| Callable[Concatenate[Context, P], T]
| Callable[Concatenate[Context, P], Coroutine[Any, Any, T]],
Expand All @@ -121,7 +121,7 @@ def run(
func: tuple[
Callable[
Concatenate[Context, P],
Coro[T],
Generator[Yieldable, Any, T],
]
| Callable[Concatenate[Context, P], T]
| Callable[Concatenate[Context, P], Coroutine[Any, Any, T]],
Expand All @@ -136,14 +136,14 @@ def run(
id: str,
func: Callable[
Concatenate[Context, P],
Coro[T],
Generator[Yieldable, Any, T],
]
| Callable[Concatenate[Context, P], T]
| Callable[Concatenate[Context, P], Coroutine[Any, Any, T]]
| tuple[
Callable[
Concatenate[Context, P],
Coro[T],
Generator[Yieldable, Any, T],
]
| Callable[Concatenate[Context, P], T]
| Callable[Concatenate[Context, P], Coroutine[Any, Any, T]],
Expand Down
3 changes: 1 addition & 2 deletions src/resonate/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
DI,
Promise[Any],
]
Coro: TypeAlias = Generator[Yieldable, Any, T]

DurableCoro: TypeAlias = Callable[Concatenate[Context, P], Coro[T]]
DurableCoro: TypeAlias = Callable[Concatenate[Context, P], Generator[Yieldable, Any, T]]
DurableSyncFn: TypeAlias = Callable[Concatenate[Context, P], T]
DurableAsyncFn: TypeAlias = Callable[Concatenate[Context, P], Coroutine[Any, Any, T]]
DurableFn: TypeAlias = Union[DurableSyncFn[P, T], DurableAsyncFn[P, T]]
Expand Down
54 changes: 27 additions & 27 deletions tests/test_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
import time
from functools import cache, lru_cache
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import pytest

Expand All @@ -22,7 +22,7 @@
from collections.abc import Generator

from resonate.context import Context
from resonate.typing import Coro, Yieldable
from resonate.typing import Yieldable


@lru_cache
Expand Down Expand Up @@ -71,7 +71,7 @@ async def foo_async(ctx: Context, n: str) -> str: # noqa: ARG001

@pytest.mark.parametrize("store", _promise_storages())
def test_golden_device_lfi(store: LocalStore | RemoteStore) -> None:
def foo_golden_device_lfi(ctx: Context, n: str) -> Coro[str]:
def foo_golden_device_lfi(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
p: Promise[str] = yield ctx.lfi(bar_golden_device_lfi, n).options(
durable=random.choice([True, False]) # noqa: S311
)
Expand All @@ -95,7 +95,7 @@ def test_factorial_lfi(store: LocalStore | RemoteStore) -> None:
def exec_id(n: int) -> str:
return f"test-factorial-lfi-{n}"

def factorial_lfi(ctx: Context, n: int) -> Coro[int]:
def factorial_lfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n == 0:
return 1
p = yield ctx.lfi(factorial_lfi, n - 1).options(
Expand All @@ -116,7 +116,7 @@ def test_fibonacci_preorder_lfi(store: LocalStore | RemoteStore) -> None:
def exec_id(n: int) -> str:
return f"test-fib-preorder-lfi-{n}"

def fib_lfi(ctx: Context, n: int) -> Coro[int]:
def fib_lfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
p1 = yield ctx.lfi(fib_lfi, n - 1).options(
Expand All @@ -143,7 +143,7 @@ def test_fibonacci_postorder_lfi(store: LocalStore | RemoteStore) -> None:
def exec_id(n: int) -> str:
return f"test-fib-postorder-lfi-{n}"

def fib_lfi(ctx: Context, n: int) -> Coro[int]:
def fib_lfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
p1 = yield ctx.lfi(fib_lfi, n - 1).options(
Expand All @@ -167,7 +167,7 @@ def fib_lfi(ctx: Context, n: int) -> Coro[int]:

@pytest.mark.parametrize("store", _promise_storages())
def test_golden_device_lfc(store: RemoteStore | LocalStore) -> None:
def foo_golden_device_lfc(ctx: Context, n: str) -> Coro[str]:
def foo_golden_device_lfc(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.lfc(bar_golden_device_lfi, n).options(
durable=random.choice([True, False]) # noqa: S311
)
Expand All @@ -189,7 +189,7 @@ def test_factorial_lfc(store: LocalStore | RemoteStore) -> None:
def exec_id(n: int) -> str:
return f"test-factorial-lfc-{n}"

def factorial_lfc(ctx: Context, n: int) -> Coro[int]:
def factorial_lfc(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n == 0:
return 1

Expand All @@ -212,7 +212,7 @@ def test_fibonacci_lfc(store: LocalStore | RemoteStore) -> None:
def exec_id(n: int) -> str:
return f"test-fib-lfc-{n}"

def fib_lfc(ctx: Context, n: int) -> Coro[int]:
def fib_lfc(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
n1 = yield ctx.lfc(fib_lfc, n - 1).options(
Expand All @@ -238,7 +238,7 @@ def fib_lfc(ctx: Context, n: int) -> Coro[int]:
def test_golden_device_rfi() -> None:
group = "test-golden-device-rfi"

def foo_golden_device_rfi(ctx: Context, n: str) -> Coro[str]:
def foo_golden_device_rfi(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
p: Promise[str] = yield ctx.rfi(bar_golden_device_rfi, n).options(
id="bar", send_to=poll(group)
)
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_factorial_rfi() -> None:
def exec_id(n: int) -> str:
return f"test-factorial-rfi-{n}"

def factorial_rfi(ctx: Context, n: int) -> Coro[int]:
def factorial_rfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n == 0:
return 1
p = yield ctx.rfi(factorial_rfi, n - 1).options(
Expand All @@ -297,7 +297,7 @@ def test_fibonacci_preorder_rfi() -> None:
def exec_id(n: int) -> str:
return f"test-fib-preorder-rfi-{n}"

def fib_rfi(ctx: Context, n: int) -> Coro[int]:
def fib_rfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
p1 = yield ctx.rfi(fib_rfi, n - 1).options(
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_fibonacci_postorder_rfi() -> None:
def exec_id(n: int) -> str:
return f"test-fib-postorder-rfi-{n}"

def fib_rfi(ctx: Context, n: int) -> Coro[int]:
def fib_rfi(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
p1 = yield ctx.rfi(fib_rfi, n - 1).options(
Expand Down Expand Up @@ -359,14 +359,14 @@ def fib_rfi(ctx: Context, n: int) -> Coro[int]:
def test_golden_device_rfi_and_lfc() -> None:
group = "test-golden-device-rfi-and-lfc"

def foo(ctx: Context, n: str) -> Coro[str]:
def foo(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.lfc(bar, n).options(
id="bar",
durable=False,
)
return v

def bar(ctx: Context, n: str) -> Coro[str]:
def bar(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
p: Promise[str] = yield ctx.rfi(baz, n).options(id="baz", send_to=poll(group))
v: str = yield p
return v
Expand Down Expand Up @@ -394,7 +394,7 @@ def test_fibonacci_full_random() -> None:
def exec_id(n: int) -> str:
return f"fib({n})"

def fib(ctx: Context, n: int) -> Coro[int]:
def fib(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n

Expand Down Expand Up @@ -447,14 +447,14 @@ def test_golden_device_rfi_and_lfc_with_decorator() -> None:
)

@resonate.register
def foo(ctx: Context, n: str) -> Coro[str]:
def foo(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.lfc(bar, n).options(
id="bar",
durable=False,
)
return v

def bar(ctx: Context, n: str) -> Coro[str]:
def bar(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
p: Promise[str] = yield ctx.rfi(baz, n).options(id="baz", send_to=poll(group))
v: str = yield p
return v
Expand All @@ -473,7 +473,7 @@ def baz(ctx: Context, n: str) -> str: # noqa: ARG001
def test_golden_device_rfc() -> None:
group = "test-golden-device-rfc"

def foo_golden_device_rfc(ctx: Context, n: str) -> Coro[str]:
def foo_golden_device_rfc(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.rfc(bar_golden_device_rfc, n).options(
id="bar", send_to=poll(group)
)
Expand Down Expand Up @@ -503,7 +503,7 @@ def test_factorial_rfc() -> None:
def exec_id(n: int) -> str:
return f"test-factorial-rfc-{n}"

def factorial_rfc(ctx: Context, n: int) -> Coro[int]:
def factorial_rfc(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n == 0:
return 1
return n * (
Expand Down Expand Up @@ -531,7 +531,7 @@ def test_fibonacci_preorder_rfc() -> None:
def exec_id(n: int) -> str:
return f"test-fib-preorder-rfc-{n}"

def fib_rfc(ctx: Context, n: int) -> Coro[int]:
def fib_rfc(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
if n <= 1:
return n
n1 = yield ctx.rfc(fib_rfc, n - 1).options(
Expand All @@ -558,14 +558,14 @@ def fib_rfc(ctx: Context, n: int) -> Coro[int]:
def test_golden_device_rfc_and_lfc() -> None:
group = "test-golden-device-rfc-and-lfc"

def foo(ctx: Context, n: str) -> Coro[str]:
def foo(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.lfc(bar, n).options(
id="bar",
durable=False,
)
return v

def bar(ctx: Context, n: str) -> Coro[str]:
def bar(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.rfc(baz, n).options(id="baz", send_to=poll(group))
return v

Expand Down Expand Up @@ -594,14 +594,14 @@ def test_golden_device_rfc_and_lfc_with_decorator() -> None:
)

@resonate.register
def foo(ctx: Context, n: str) -> Coro[str]:
def foo(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.lfc(bar, n).options(
id="bar",
durable=False,
)
return v

def bar(ctx: Context, n: str) -> Coro[str]:
def bar(ctx: Context, n: str) -> Generator[Yieldable, Any, str]:
v: str = yield ctx.rfc(baz, n).options(id="baz", send_to=poll(group))
return v

Expand Down Expand Up @@ -668,7 +668,7 @@ def test_human_in_the_loop() -> None:
def _user_manual_completion(id: str) -> DurablePromise:
return DurablePromise(id=id)

def human_in_the_loop(ctx: Context) -> Coro[str]:
def human_in_the_loop(ctx: Context) -> Generator[Yieldable, Any, str]:
name: str = yield ctx.rfc(
_user_manual_completion("test-human-in-loop-question-to-answer-1")
)
Expand Down Expand Up @@ -708,7 +708,7 @@ def test_sleep() -> None:
s = Resonate(store=store, task_source=Poller("http://localhost:8002", group=group))

@s.register
def foo_sleep(ctx: Context, n: int) -> Coro[int]:
def foo_sleep(ctx: Context, n: int) -> Generator[Yieldable, Any, int]:
yield ctx.sleep(n)
return n

Expand Down

0 comments on commit 2dbec99

Please sign in to comment.