|
15 | 15 |
|
16 | 16 | import abc |
17 | 17 | from dataclasses import dataclass |
18 | | -from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar, Union, Coroutine, overload |
| 18 | +from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar, Union, Coroutine, overload, ParamSpec |
19 | 19 | import typing |
20 | 20 | from datetime import timedelta |
| 21 | + |
| 22 | +import typing_extensions |
21 | 23 | from restate.serde import DefaultSerde, Serde |
22 | 24 |
|
23 | 25 | T = TypeVar('T') |
24 | 26 | I = TypeVar('I') |
25 | 27 | O = TypeVar('O') |
| 28 | +P = ParamSpec('P') |
26 | 29 |
|
27 | | -RunAction = Union[Callable[..., Coroutine[Any, Any, T]], Callable[..., T]] |
28 | 30 | HandlerType = Union[Callable[[Any, I], Awaitable[O]], Callable[[Any], Awaitable[O]]] |
| 31 | +RunAction = Union[Callable[..., Coroutine[Any, Any, T]], Callable[..., T]] |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class RunOptions(typing.Generic[T]): |
| 35 | + """ |
| 36 | + Options for running an action. |
| 37 | + """ |
| 38 | + |
| 39 | + serde: Serde[T] = DefaultSerde() |
| 40 | + """The serialization/deserialization mechanism. - if the default serde is used, a default serializer will be used based on the type. |
| 41 | + See also 'type_hint'.""" |
| 42 | + max_attempts: Optional[int] = None |
| 43 | + """The maximum number of retry attempts to complete the action. |
| 44 | + If None, the action will be retried indefinitely, until it succeeds. |
| 45 | + Otherwise, the action will be retried until the maximum number of attempts is reached and then it will raise a TerminalError.""" |
| 46 | + max_retry_duration: Optional[timedelta] = None |
| 47 | + """The maximum duration for retrying. If None, the action will be retried indefinitely, until it succeeds. |
| 48 | + Otherwise, the action will be retried until the maximum duration is reached and then it will raise a TerminalError.""" |
| 49 | + type_hint: Optional[typing.Type[T]] = None |
| 50 | + """The type hint of the return value of the action. This is used to pick the serializer. If None, the type hint will be inferred from the action's return type, or the provided serializer.""" |
29 | 51 |
|
30 | 52 | # pylint: disable=R0903 |
31 | 53 | class RestateDurableFuture(typing.Generic[T], Awaitable[T]): |
@@ -197,6 +219,8 @@ def request(self) -> Request: |
197 | 219 | Returns the request object. |
198 | 220 | """ |
199 | 221 |
|
| 222 | + |
| 223 | + @typing_extensions.deprecated("`run` is deprecated, use `run_typed` instead for better type safety") |
200 | 224 | @overload |
201 | 225 | @abc.abstractmethod |
202 | 226 | def run(self, |
@@ -226,6 +250,7 @@ def run(self, |
226 | 250 |
|
227 | 251 | """ |
228 | 252 |
|
| 253 | + @typing_extensions.deprecated("`run` is deprecated, use `run_typed` instead for better type safety") |
229 | 254 | @overload |
230 | 255 | @abc.abstractmethod |
231 | 256 | def run(self, |
@@ -255,6 +280,7 @@ def run(self, |
255 | 280 |
|
256 | 281 | """ |
257 | 282 |
|
| 283 | + @typing_extensions.deprecated("`run` is deprecated, use `run_typed` instead for better type safety") |
258 | 284 | @abc.abstractmethod |
259 | 285 | def run(self, |
260 | 286 | name: str, |
@@ -283,6 +309,73 @@ def run(self, |
283 | 309 |
|
284 | 310 | """ |
285 | 311 |
|
| 312 | + |
| 313 | + @overload |
| 314 | + @abc.abstractmethod |
| 315 | + def run_typed(self, |
| 316 | + name: str, |
| 317 | + action: Callable[P, Coroutine[Any, Any,T]], |
| 318 | + options: RunOptions[T] = RunOptions(), |
| 319 | + /, |
| 320 | + *args: P.args, |
| 321 | + **kwargs: P.kwargs, |
| 322 | + ) -> RestateDurableFuture[T]: |
| 323 | + """ |
| 324 | + Typed version of run that provides type hints for the function arguments. |
| 325 | + Runs the given action with the given name. |
| 326 | +
|
| 327 | + Args: |
| 328 | + name: The name of the action. |
| 329 | + action: The action to run. |
| 330 | + options: The options for the run. |
| 331 | + *args: The arguments to pass to the action. |
| 332 | + **kwargs: The keyword arguments to pass to the action. |
| 333 | + """ |
| 334 | + |
| 335 | + @overload |
| 336 | + @abc.abstractmethod |
| 337 | + def run_typed(self, |
| 338 | + name: str, |
| 339 | + action: Callable[P, T], |
| 340 | + options: RunOptions[T] = RunOptions(), |
| 341 | + /, |
| 342 | + *args: P.args, |
| 343 | + **kwargs: P.kwargs, |
| 344 | + ) -> RestateDurableFuture[T]: |
| 345 | + """ |
| 346 | + Typed version of run that provides type hints for the function arguments. |
| 347 | + Runs the given coroutine action with the given name. |
| 348 | +
|
| 349 | + Args: |
| 350 | + name: The name of the action. |
| 351 | + action: The action to run. |
| 352 | + options: The options for the run. |
| 353 | + *args: The arguments to pass to the action. |
| 354 | + **kwargs: The keyword arguments to pass to the action. |
| 355 | + """ |
| 356 | + |
| 357 | + @abc.abstractmethod |
| 358 | + def run_typed(self, |
| 359 | + name: str, |
| 360 | + action: Union[Callable[P, Coroutine[Any, Any, T]], Callable[P, T]], |
| 361 | + options: RunOptions[T] = RunOptions(), |
| 362 | + /, |
| 363 | + *args: P.args, |
| 364 | + **kwargs: P.kwargs, |
| 365 | + ) -> RestateDurableFuture[T]: |
| 366 | + """ |
| 367 | + Typed version of run that provides type hints for the function arguments. |
| 368 | + Runs the given action with the given name. |
| 369 | +
|
| 370 | + Args: |
| 371 | + name: The name of the action. |
| 372 | + action: The action to run. |
| 373 | + options: The options for the run. |
| 374 | + *args: The arguments to pass to the action. |
| 375 | + **kwargs: The keyword arguments to pass to the action. |
| 376 | +
|
| 377 | + """ |
| 378 | + |
286 | 379 | @abc.abstractmethod |
287 | 380 | def sleep(self, delta: timedelta) -> RestateDurableSleepFuture: |
288 | 381 | """ |
|
0 commit comments