Skip to content

Commit e50edde

Browse files
committed
respond to comments
Signed-off-by: Tim Li <[email protected]>
1 parent f39f403 commit e50edde

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

cadence/client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import socket
33
import uuid
44
from datetime import timedelta
5-
from typing import TypedDict, Unpack, Any, cast, Union, Callable
5+
from typing import TypedDict, Unpack, Any, cast, Union
66

77
from grpc import ChannelCredentials, Compression
88
from google.protobuf.duration_pb2 import Duration
@@ -24,6 +24,7 @@
2424
from cadence.api.v1.tasklist_pb2 import TaskList
2525
from cadence.data_converter import DataConverter, DefaultDataConverter
2626
from cadence.metrics import MetricsEmitter, NoOpMetricsEmitter
27+
from cadence.workflow import WorkflowDefinition
2728

2829

2930
class StartWorkflowOptions(TypedDict, total=False):
@@ -134,7 +135,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
134135

135136
async def _build_start_workflow_request(
136137
self,
137-
workflow: Union[str, Callable],
138+
workflow: Union[str, WorkflowDefinition],
138139
args: tuple[Any, ...],
139140
options: StartWorkflowOptions,
140141
) -> StartWorkflowExecutionRequest:
@@ -146,8 +147,8 @@ async def _build_start_workflow_request(
146147
if isinstance(workflow, str):
147148
workflow_type_name = workflow
148149
else:
149-
# For callable, use function name or __name__ attribute
150-
workflow_type_name = getattr(workflow, "__name__", str(workflow))
150+
# For WorkflowDefinition, use the name property
151+
workflow_type_name = workflow.name
151152

152153
# Encode input arguments
153154
input_payload = None
@@ -188,15 +189,15 @@ async def _build_start_workflow_request(
188189

189190
async def start_workflow(
190191
self,
191-
workflow: Union[str, Callable],
192+
workflow: Union[str, WorkflowDefinition],
192193
*args,
193194
**options_kwargs: Unpack[StartWorkflowOptions],
194195
) -> WorkflowExecution:
195196
"""
196197
Start a workflow execution asynchronously.
197198
198199
Args:
199-
workflow: Workflow function or workflow type name string
200+
workflow: WorkflowDefinition or workflow type name string
200201
*args: Arguments to pass to the workflow
201202
**options_kwargs: StartWorkflowOptions as keyword arguments
202203
@@ -233,7 +234,7 @@ async def start_workflow(
233234

234235
async def signal_with_start_workflow(
235236
self,
236-
workflow: Union[str, Callable],
237+
workflow: Union[str, WorkflowDefinition],
237238
signal_name: str,
238239
signal_input: Any = None,
239240
*args,
@@ -243,7 +244,7 @@ async def signal_with_start_workflow(
243244
Signal a workflow execution, starting it if it is not already running.
244245
245246
Args:
246-
workflow: Workflow function or workflow type name string
247+
workflow: WorkflowDefinition or workflow type name string
247248
signal_name: Name of the signal
248249
signal_input: Input data for the signal
249250
*args: Arguments to pass to the workflow if it needs to be started

cadence/workflow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
Any,
1313
Optional,
1414
Union,
15+
TYPE_CHECKING,
1516
)
1617
import inspect
1718

18-
from cadence.client import Client
19+
if TYPE_CHECKING:
20+
from cadence.client import Client
1921

2022
T = TypeVar("T", bound=Callable[..., Any])
2123

@@ -152,7 +154,7 @@ class WorkflowContext(ABC):
152154
def info(self) -> WorkflowInfo: ...
153155

154156
@abstractmethod
155-
def client(self) -> Client: ...
157+
def client(self) -> "Client": ...
156158

157159
@contextmanager
158160
def _activate(self) -> Iterator[None]:

tests/cadence/test_client_workflow.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from cadence.client import Client, StartWorkflowOptions, _validate_and_apply_defaults
1212
from cadence.data_converter import DefaultDataConverter
13+
from cadence.workflow import WorkflowDefinition, WorkflowDefinitionOptions
1314

1415

1516
@pytest.fixture
@@ -96,11 +97,17 @@ async def test_build_request_with_string_workflow(self, mock_client):
9697
uuid.UUID(request.request_id) # This will raise if not valid UUID
9798

9899
@pytest.mark.asyncio
99-
async def test_build_request_with_callable_workflow(self, mock_client):
100-
"""Test building request with callable workflow."""
100+
async def test_build_request_with_workflow_definition(self, mock_client):
101+
"""Test building request with WorkflowDefinition."""
102+
from cadence import workflow
101103

102-
def test_workflow():
103-
pass
104+
class TestWorkflow:
105+
@workflow.run
106+
async def run(self):
107+
pass
108+
109+
workflow_opts = WorkflowDefinitionOptions(name="test_workflow")
110+
workflow_definition = WorkflowDefinition.wrap(TestWorkflow, workflow_opts)
104111

105112
client = Client(domain="test-domain", target="localhost:7933")
106113

@@ -110,7 +117,9 @@ def test_workflow():
110117
task_start_to_close_timeout=timedelta(seconds=30),
111118
)
112119

113-
request = await client._build_start_workflow_request(test_workflow, (), options)
120+
request = await client._build_start_workflow_request(
121+
workflow_definition, (), options
122+
)
114123

115124
assert request.workflow_type.name == "test_workflow"
116125

0 commit comments

Comments
 (0)