-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Define workflow signal decorator and definition for registry #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+335
−2
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f0259d
feat: define workflow signal decorator in workflowDefinition
timl3136 992534f
lint + formatter
timl3136 e02576e
minor lint
timl3136 db46c85
implement signalDefinition
timl3136 ab25568
Merge branch 'main' of github.com:cadence-workflow/cadence-python-cli…
timl3136 0bd5e04
Merge branch 'main' into signal_decorator
timl3136 49e71de
Merge upstream/main into signal_decorator
timl3136 a4196eb
address comments
timl3136 acce71e
minor typing
timl3136 a21c194
Merge branch 'main' into signal_decorator
timl3136 0a2f1b1
Merge remote-tracking branch 'upstream/main' into signal_decorator
timl3136 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| """ | ||
| Signal definition and registration for Cadence workflows. | ||
|
|
||
| This module provides functionality to define and register signal handlers | ||
| for workflows, similar to ActivityDefinition but for signals. | ||
| """ | ||
|
|
||
| import inspect | ||
| from dataclasses import dataclass | ||
| from functools import update_wrapper | ||
| from inspect import Parameter, signature | ||
| from typing import ( | ||
| Callable, | ||
| Generic, | ||
| ParamSpec, | ||
| Type, | ||
| TypeVar, | ||
| TypedDict, | ||
| Unpack, | ||
| overload, | ||
| get_type_hints, | ||
| Any, | ||
| ) | ||
|
|
||
| P = ParamSpec("P") | ||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SignalParameter: | ||
| """Parameter metadata for a signal handler.""" | ||
|
|
||
| name: str | ||
| type_hint: Type | None | ||
| has_default: bool | ||
| default_value: Any | ||
|
|
||
|
|
||
| class SignalDefinitionOptions(TypedDict, total=False): | ||
| """Options for defining a signal.""" | ||
|
|
||
| name: str | ||
|
|
||
|
|
||
| class SignalDefinition(Generic[P, T]): | ||
| """ | ||
| Definition of a signal handler with metadata. | ||
|
|
||
| Similar to ActivityDefinition but for signal handlers. | ||
| Provides type safety and metadata for signal handlers. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| wrapped: Callable[P, T], | ||
| name: str, | ||
| params: list[SignalParameter], | ||
| is_async: bool, | ||
| ): | ||
| self._wrapped = wrapped | ||
| self._name = name | ||
| self._params = params | ||
| self._is_async = is_async | ||
| update_wrapper(self, wrapped) | ||
|
|
||
| def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T: | ||
| """Call the wrapped signal handler function.""" | ||
| return self._wrapped(*args, **kwargs) | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Get the signal name.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def params(self) -> list[SignalParameter]: | ||
| """Get the signal parameters.""" | ||
| return self._params | ||
|
|
||
| @property | ||
| def is_async(self) -> bool: | ||
| """Check if the signal handler is async.""" | ||
| return self._is_async | ||
|
|
||
| @property | ||
| def wrapped(self) -> Callable[P, T]: | ||
| """Get the wrapped signal handler function.""" | ||
| return self._wrapped | ||
|
|
||
| @staticmethod | ||
| def wrap( | ||
| fn: Callable[P, T], opts: SignalDefinitionOptions | ||
| ) -> "SignalDefinition[P, T]": | ||
| """ | ||
| Wrap a function as a SignalDefinition. | ||
|
|
||
| Args: | ||
| fn: The signal handler function to wrap | ||
| opts: Options for the signal definition | ||
|
|
||
| Returns: | ||
| A SignalDefinition instance | ||
|
|
||
| Raises: | ||
| ValueError: If name is not provided in options or return type is not None | ||
| """ | ||
| name = opts.get("name") or fn.__qualname__ | ||
| is_async = inspect.iscoroutinefunction(fn) | ||
| params = _get_signal_signature(fn) | ||
| _validate_signal_return_type(fn) | ||
|
|
||
| return SignalDefinition(fn, name, params, is_async) | ||
|
|
||
|
|
||
| SignalDecorator = Callable[[Callable[P, T]], SignalDefinition[P, T]] | ||
|
|
||
|
|
||
| @overload | ||
| def defn(fn: Callable[P, T]) -> SignalDefinition[P, T]: ... | ||
|
|
||
|
|
||
| @overload | ||
| def defn(**kwargs: Unpack[SignalDefinitionOptions]) -> SignalDecorator: ... | ||
|
|
||
|
|
||
| def defn( | ||
| fn: Callable[P, T] | None = None, **kwargs: Unpack[SignalDefinitionOptions] | ||
| ) -> SignalDecorator | SignalDefinition[P, T]: | ||
| """ | ||
| Decorator to define a signal handler. | ||
|
|
||
| Can be used with or without parentheses: | ||
| @signal.defn(name="approval") | ||
| async def handle_approval(self, approved: bool): | ||
| ... | ||
|
|
||
| @signal.defn(name="approval") | ||
| def handle_approval(self, approved: bool): | ||
| ... | ||
|
|
||
| Args: | ||
| fn: The signal handler function to decorate | ||
| **kwargs: Options for the signal definition (name is required) | ||
|
|
||
| Returns: | ||
| The decorated function as a SignalDefinition instance | ||
|
|
||
| Raises: | ||
| ValueError: If name is not provided | ||
| """ | ||
| options = SignalDefinitionOptions(**kwargs) | ||
|
|
||
| def decorator(inner_fn: Callable[P, T]) -> SignalDefinition[P, T]: | ||
| return SignalDefinition.wrap(inner_fn, options) | ||
|
|
||
| if fn is not None: | ||
| return decorator(fn) | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| def _validate_signal_return_type(fn: Callable) -> None: | ||
| """ | ||
| Validate that signal handler returns None. | ||
|
|
||
| Args: | ||
| fn: The signal handler function | ||
|
|
||
| Raises: | ||
| ValueError: If return type is not None | ||
| """ | ||
| try: | ||
| hints = get_type_hints(fn) | ||
| ret_type = hints.get("return", inspect.Signature.empty) | ||
|
|
||
| if ret_type is not None and ret_type is not inspect.Signature.empty: | ||
| raise ValueError( | ||
| f"Signal handler '{fn.__qualname__}' must return None " | ||
| f"(signals cannot return values), got {ret_type}" | ||
| ) | ||
| except NameError: | ||
| pass | ||
|
|
||
|
|
||
| def _get_signal_signature(fn: Callable[P, T]) -> list[SignalParameter]: | ||
| """ | ||
| Extract parameter information from a signal handler function. | ||
|
|
||
| Args: | ||
| fn: The signal handler function | ||
|
|
||
| Returns: | ||
| List of SignalParameter objects | ||
|
|
||
| Raises: | ||
| ValueError: If parameters are not positional | ||
| """ | ||
| sig = signature(fn) | ||
| args = sig.parameters | ||
| hints = get_type_hints(fn) | ||
| params = [] | ||
|
|
||
| for name, param in args.items(): | ||
| # Filter out the self parameter for instance methods | ||
| if param.name == "self": | ||
| continue | ||
|
|
||
| has_default = param.default != Parameter.empty | ||
| default = param.default if has_default else None | ||
|
|
||
| if param.kind in (Parameter.POSITIONAL_ONLY, Parameter.POSITIONAL_OR_KEYWORD): | ||
| type_hint = hints.get(name, None) | ||
| params.append(SignalParameter(name, type_hint, has_default, default)) | ||
| else: | ||
| raise ValueError( | ||
| f"Signal handler '{fn.__qualname__}' parameter '{name}' must be positional, " | ||
| f"got {param.kind.name}" | ||
| ) | ||
|
|
||
| return params | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.