|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import functools |
4 | | -from collections.abc import AsyncGenerator |
5 | | -from typing import TYPE_CHECKING, Concatenate, Generic, Literal, get_args, get_origin |
6 | | -from typing_extensions import Any, ParamSpec, TypeVar, final, overload |
7 | | - |
8 | | -from duron.typing import UnspecifiedType, inspect_function |
| 3 | +from typing import TYPE_CHECKING |
| 4 | +from typing_extensions import NamedTuple, ParamSpec, TypeVar, overload |
9 | 5 |
|
10 | 6 | if TYPE_CHECKING: |
11 | | - from collections.abc import Callable, Coroutine |
12 | | - |
13 | | - from duron.typing import TypeHint |
| 7 | + from collections.abc import Callable |
14 | 8 |
|
15 | 9 |
|
16 | | -_T = TypeVar("_T") |
17 | | -_S = TypeVar("_S") |
18 | 10 | _T_co = TypeVar("_T_co", covariant=True) |
19 | 11 | _P = ParamSpec("_P") |
20 | 12 |
|
21 | 13 |
|
22 | | -@final |
23 | | -class EffectFn(Generic[_P, _T_co]): |
24 | | - def __init__(self, fn: Callable[_P, Coroutine[Any, Any, _T_co]]) -> None: |
25 | | - self.fn = fn |
26 | | - self.type_hint = inspect_function(fn) |
27 | | - functools.update_wrapper(self, fn) |
28 | | - |
29 | | - def __call__( |
30 | | - self, *args: _P.args, **kwargs: _P.kwargs |
31 | | - ) -> Coroutine[Any, Any, _T_co]: |
32 | | - return self.fn(*args, **kwargs) |
33 | | - |
34 | | - |
35 | | -@final |
36 | | -class StatefulFn(Generic[_P, _S, _T]): |
37 | | - def __init__( |
38 | | - self, |
39 | | - fn: Callable[Concatenate[_S, _P], AsyncGenerator[_T, _S]], |
40 | | - initial: Callable[[], _S], |
41 | | - reducer: Callable[[_S, _T], _S], |
42 | | - ) -> None: |
43 | | - self.fn = fn |
44 | | - self.type_hint = inspect_function(fn) |
45 | | - self.initial = initial |
46 | | - self.reducer = reducer |
47 | | - |
48 | | - action_type: TypeHint[_T] = UnspecifiedType |
49 | | - if get_origin(ret := self.type_hint.return_type) is AsyncGenerator: |
50 | | - action_type, _ = get_args(ret) |
51 | | - self.action_type = action_type |
52 | | - functools.update_wrapper(self, fn) |
53 | | - |
54 | | - def __call__( |
55 | | - self, state: _S, *args: _P.args, **kwargs: _P.kwargs |
56 | | - ) -> AsyncGenerator[_T, _S]: |
57 | | - return self.fn(state, *args, **kwargs) |
| 14 | +class Reducer(NamedTuple): |
| 15 | + """Annotation to mark a parameter as a reducer.""" |
| 16 | + |
| 17 | + reducer: Callable[[object, object], object] |
58 | 18 |
|
59 | 19 |
|
60 | 20 | @overload |
61 | | -def effect(fn: Callable[_P, Coroutine[Any, Any, _T_co]], /) -> EffectFn[_P, _T_co]: ... |
62 | | -@overload |
63 | | -def effect( |
64 | | - *, stateful: Literal[False] = ... |
65 | | -) -> Callable[[Callable[_P, Coroutine[Any, Any, _T_co]]], EffectFn[_P, _T_co]]: ... |
| 21 | +def effect(fn: Callable[_P, _T_co], /) -> Callable[_P, _T_co]: ... |
66 | 22 | @overload |
| 23 | +def effect() -> Callable[[Callable[_P, _T_co]], Callable[_P, _T_co]]: ... |
67 | 24 | def effect( |
68 | | - *, |
69 | | - stateful: Literal[True], |
70 | | - initial: Callable[[], _S], |
71 | | - reducer: Callable[[_S, _T], _S], |
72 | | -) -> Callable[ |
73 | | - [Callable[Concatenate[_S, _P], AsyncGenerator[_T, _S]]], StatefulFn[_P, _S, _T] |
74 | | -]: ... |
75 | | -def effect( |
76 | | - fn: Callable[_P, Coroutine[Any, Any, _T_co]] | None = None, |
77 | | - /, |
78 | | - *, |
79 | | - # stateful parameters |
80 | | - stateful: bool = False, |
81 | | - initial: Callable[[], _S] | None = None, |
82 | | - reducer: Callable[[_S, _T], _S] | None = None, |
83 | | -) -> ( |
84 | | - EffectFn[_P, _T_co] |
85 | | - | Callable[[Callable[_P, Coroutine[Any, Any, _T_co]]], EffectFn[_P, _T_co]] |
86 | | - | Callable[ |
87 | | - [Callable[Concatenate[_S, _P], AsyncGenerator[_T, _S]]], StatefulFn[_P, _S, _T] |
88 | | - ] |
89 | | -): |
| 25 | + fn: Callable[_P, _T_co] | None = None, / |
| 26 | +) -> Callable[_P, _T_co] | Callable[[Callable[_P, _T_co]], Callable[_P, _T_co]]: |
90 | 27 | """Decorator to mark async functions as effects. |
91 | 28 |
|
92 | 29 | Effects are operations that interact with the outside world. |
93 | 30 |
|
94 | | - Args: |
95 | | - stateful: Whether the effect is stateful. |
96 | | - initial: Factory function for initial state (required with `stateful=True`) |
97 | | - reducer: Function to reduce actions into state (required with `stateful=True`) |
98 | | -
|
99 | 31 | Example: |
100 | | - Basic example: |
101 | 32 | ```python |
102 | 33 | @duron.effect |
103 | | - async def fetch_data(url: str) -> dict: |
104 | | - return await http_client.get(url) |
105 | | - ``` |
| 34 | + async def send_email(to: str, subject: str, body: str) -> None: |
| 35 | + # Send an email |
| 36 | + ... |
106 | 37 |
|
107 | | - Stateful example: |
108 | | - ```python |
109 | | - @duron.effect(stateful=True, initial=lambda: 0, reducer=int.__add__) |
110 | | - async def count_items(state: int, items: list) -> AsyncGenerator[int, int]: |
111 | | - # restore based on `state` |
112 | | - for item in items: |
113 | | - yield 1 |
| 38 | +
|
| 39 | + @duron.effect |
| 40 | + async def counter( |
| 41 | + state: Annotated[int, duron.Reducer(lambda s, a: s + a)], increment: int |
| 42 | + ) -> AsyncGenerator[int, int]: |
| 43 | + state += increment |
| 44 | + yield state |
114 | 45 | ``` |
115 | 46 |
|
| 47 | +
|
116 | 48 | Returns: |
117 | 49 | Function wrapper that can be invoked with [ctx.run()][duron.Context.run] |
118 | | -
|
119 | | - Raises: |
120 | | - ValueError: If stateful is True but initial or reducer is not provided. |
121 | 50 | """ |
122 | | - if fn is not None: |
123 | | - return EffectFn(fn) |
124 | 51 |
|
125 | | - if stateful: |
126 | | - if not initial or not reducer: |
127 | | - msg = "initial and reducer must be provided for stateful ops" |
128 | | - raise ValueError(msg) |
129 | | - |
130 | | - def decorate_stateful( |
131 | | - fn: Callable[Concatenate[_S, _P], AsyncGenerator[_T, _S]], |
132 | | - ) -> StatefulFn[_P, _S, _T]: |
133 | | - return StatefulFn(fn, initial, reducer) |
| 52 | + if fn is not None: |
| 53 | + return fn |
134 | 54 |
|
135 | | - return decorate_stateful |
| 55 | + def decorate(fn: Callable[_P, _T_co]) -> Callable[_P, _T_co]: |
| 56 | + return fn |
136 | 57 |
|
137 | | - return EffectFn |
| 58 | + return decorate |
0 commit comments