Skip to content

Commit 7ed53fb

Browse files
committed
Test fails as expected due to not wrapping non-async def op result
1 parent 4361f76 commit 7ed53fb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

temporalio/worker/_nexus.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,27 @@ async def run() -> temporalio.bridge.proto.nexus.NexusTaskCompletion:
308308
)
309309
)
310310
else:
311-
raise TypeError(
311+
# TODO(dan): what should the error response be when the user has failed to wrap their return type?
312+
# TODO(dan): unify this failure completion with the path above
313+
err = TypeError(
312314
"Nexus operation must return either nexusrpc.StartOperationResultSync "
313315
"or nexusrpc.StartOperationResultAsync"
314316
)
317+
handler_err = _exception_to_handler_error(err)
318+
return temporalio.bridge.proto.nexus.NexusTaskCompletion(
319+
task_token=task_token,
320+
error=temporalio.api.nexus.v1.HandlerError(
321+
error_type=handler_err.type.value,
322+
failure=await self._exception_to_failure_proto(
323+
handler_err.__cause__
324+
),
325+
retry_behavior=(
326+
temporalio.api.enums.v1.NexusHandlerErrorRetryBehavior.NEXUS_HANDLER_ERROR_RETRY_BEHAVIOR_RETRYABLE
327+
if handler_err.retryable
328+
else temporalio.api.enums.v1.NexusHandlerErrorRetryBehavior.NEXUS_HANDLER_ERROR_RETRY_BEHAVIOR_NON_RETRYABLE
329+
),
330+
),
331+
)
315332

316333
return temporalio.bridge.proto.nexus.NexusTaskCompletion(
317334
task_token=task_token,

tests/worker/test_nexus_handler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class MyService:
118118
Input, Output
119119
]
120120
sync_operation_without_type_annotations: nexusrpc.interface.Operation[Input, Output]
121+
sync_operation_with_non_async_def: nexusrpc.interface.Operation[Input, Output]
121122
non_retryable_application_error: nexusrpc.interface.Operation[Input, Output]
122123
retryable_application_error: nexusrpc.interface.Operation[Input, Output]
123124
check_operation_timeout_header: nexusrpc.interface.Operation[Input, Output]
@@ -224,6 +225,12 @@ async def async_operation(
224225
options=options,
225226
)
226227

228+
@nexusrpc.handler.sync_operation
229+
def sync_operation_with_non_async_def(
230+
self, input: Input, options: nexusrpc.handler.StartOperationOptions
231+
) -> Output:
232+
return Output(value=f"from start method: {input.value}")
233+
227234
@nexusrpc.handler.sync_operation
228235
async def sync_operation_without_type_annotations(self, input, options):
229236
return Output(
@@ -376,6 +383,15 @@ class SyncHandlerHappyPath(_TestCase):
376383
# "Nexus-Link header not echoed correctly."
377384

378385

386+
class SyncHandlerHappyPathNonAsyncDef(_TestCase):
387+
operation = "sync_operation_with_non_async_def"
388+
input = Input("hello")
389+
expected_response = SuccessfulResponse(
390+
status_code=200,
391+
body_json={"value": "from start method: hello"},
392+
)
393+
394+
379395
class SyncHandlerHappyPathWithoutTypeAnnotations(_TestCase):
380396
operation = "sync_operation_without_type_annotations"
381397
input = Input("hello")
@@ -502,6 +518,7 @@ class OperationError(_FailureTestCase):
502518
"test_case",
503519
[
504520
SyncHandlerHappyPath,
521+
SyncHandlerHappyPathNonAsyncDef,
505522
SyncHandlerHappyPathWithoutTypeAnnotations,
506523
AsyncHandlerHappyPath,
507524
AsyncHandlerHappyPathWithoutTypeAnnotations,

0 commit comments

Comments
 (0)