-
Notifications
You must be signed in to change notification settings - Fork 110
Add tests for type-checker false negatives #976
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
This file was deleted.
Oops, something went wrong.
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,207 @@ | ||
""" | ||
This file exists to test for type-checker false positives and false negatives. | ||
It doesn't contain any test functions. | ||
""" | ||
|
||
from dataclasses import dataclass | ||
|
||
import nexusrpc | ||
|
||
import temporalio.nexus | ||
from temporalio import workflow | ||
|
||
|
||
@dataclass | ||
class MyInput: | ||
pass | ||
|
||
|
||
@dataclass | ||
class MyOutput: | ||
pass | ||
|
||
|
||
@nexusrpc.service | ||
class MyService: | ||
my_sync_operation: nexusrpc.Operation[MyInput, MyOutput] | ||
my_workflow_run_operation: nexusrpc.Operation[MyInput, MyOutput] | ||
|
||
|
||
@nexusrpc.handler.service_handler(service=MyService) | ||
class MyServiceHandler: | ||
@nexusrpc.handler.sync_operation | ||
async def my_sync_operation( | ||
self, _ctx: nexusrpc.handler.StartOperationContext, _input: MyInput | ||
) -> MyOutput: | ||
raise NotImplementedError | ||
|
||
@temporalio.nexus.workflow_run_operation | ||
async def my_workflow_run_operation( | ||
self, _ctx: temporalio.nexus.WorkflowRunOperationContext, _input: MyInput | ||
) -> temporalio.nexus.WorkflowHandle[MyOutput]: | ||
raise NotImplementedError | ||
|
||
|
||
@nexusrpc.handler.service_handler(service=MyService) | ||
class MyServiceHandler2: | ||
@nexusrpc.handler.sync_operation | ||
async def my_sync_operation( | ||
self, _ctx: nexusrpc.handler.StartOperationContext, _input: MyInput | ||
) -> MyOutput: | ||
raise NotImplementedError | ||
|
||
@temporalio.nexus.workflow_run_operation | ||
async def my_workflow_run_operation( | ||
self, _ctx: temporalio.nexus.WorkflowRunOperationContext, _input: MyInput | ||
) -> temporalio.nexus.WorkflowHandle[MyOutput]: | ||
raise NotImplementedError | ||
|
||
|
||
@nexusrpc.handler.service_handler | ||
class MyServiceHandlerWithoutServiceDefinition: | ||
@nexusrpc.handler.sync_operation | ||
async def my_sync_operation( | ||
self, _ctx: nexusrpc.handler.StartOperationContext, _input: MyInput | ||
) -> MyOutput: | ||
raise NotImplementedError | ||
|
||
@temporalio.nexus.workflow_run_operation | ||
async def my_workflow_run_operation( | ||
self, _ctx: temporalio.nexus.WorkflowRunOperationContext, _input: MyInput | ||
) -> temporalio.nexus.WorkflowHandle[MyOutput]: | ||
raise NotImplementedError | ||
|
||
|
||
@workflow.defn | ||
class MyWorkflow1: | ||
@workflow.run | ||
async def test_invoke_by_operation_definition_happy_path(self) -> None: | ||
""" | ||
When a nexus client calls an operation by referencing an operation definition on | ||
a service definition, the output type is inferred correctly. | ||
""" | ||
nexus_client = workflow.create_nexus_client( | ||
service=MyService, | ||
endpoint="fake-endpoint", | ||
) | ||
input = MyInput() | ||
|
||
# sync operation | ||
_output_1: MyOutput = await nexus_client.execute_operation( | ||
MyService.my_sync_operation, input | ||
) | ||
_handle_1: workflow.NexusOperationHandle[ | ||
MyOutput | ||
] = await nexus_client.start_operation(MyService.my_sync_operation, input) | ||
_output_1_1: MyOutput = await _handle_1 | ||
|
||
# workflow run operation | ||
_output_2: MyOutput = await nexus_client.execute_operation( | ||
MyService.my_workflow_run_operation, input | ||
) | ||
_handle_2: workflow.NexusOperationHandle[ | ||
MyOutput | ||
] = await nexus_client.start_operation( | ||
MyService.my_workflow_run_operation, input | ||
) | ||
_output_2_1: MyOutput = await _handle_2 | ||
|
||
|
||
@workflow.defn | ||
class MyWorkflow2: | ||
@workflow.run | ||
async def test_invoke_by_operation_handler_happy_path(self) -> None: | ||
""" | ||
When a nexus client calls an operation by referencing an operation handler on a | ||
service handler, the output type is inferred correctly. | ||
""" | ||
nexus_client = workflow.create_nexus_client( | ||
service=MyServiceHandler, # MyService would also work | ||
endpoint="fake-endpoint", | ||
) | ||
input = MyInput() | ||
|
||
# sync operation | ||
_output_1: MyOutput = await nexus_client.execute_operation( | ||
MyServiceHandler.my_sync_operation, input | ||
) | ||
_handle_1: workflow.NexusOperationHandle[ | ||
MyOutput | ||
] = await nexus_client.start_operation( | ||
MyServiceHandler.my_sync_operation, input | ||
) | ||
_output_1_1: MyOutput = await _handle_1 | ||
|
||
# workflow run operation | ||
_output_2: MyOutput = await nexus_client.execute_operation( | ||
MyServiceHandler.my_workflow_run_operation, input | ||
) | ||
_handle_2: workflow.NexusOperationHandle[ | ||
MyOutput | ||
] = await nexus_client.start_operation( | ||
MyServiceHandler.my_workflow_run_operation, input | ||
) | ||
_output_2_1: MyOutput = await _handle_2 | ||
|
||
|
||
@workflow.defn | ||
class MyWorkflow3: | ||
@workflow.run | ||
async def test_invoke_by_operation_definition_wrong_input_type(self) -> None: | ||
""" | ||
When a nexus client calls an operation by referencing an operation definition on | ||
a service definition, there is a type error if the input type is wrong. | ||
""" | ||
nexus_client = workflow.create_nexus_client( | ||
service=MyService, | ||
endpoint="fake-endpoint", | ||
) | ||
# assert-type-error-pyright: 'No overloads for "execute_operation" match' | ||
await nexus_client.execute_operation( # type: ignore | ||
MyService.my_sync_operation, | ||
# assert-type-error-pyright: 'Argument of type .+ cannot be assigned to parameter "input"' | ||
"wrong-input-type", # type: ignore | ||
) | ||
|
||
|
||
@workflow.defn | ||
class MyWorkflow4: | ||
@workflow.run | ||
async def test_invoke_by_operation_handler_wrong_input_type(self) -> None: | ||
""" | ||
When a nexus client calls an operation by referencing an operation handler on a | ||
service handler, there is a type error if the input type is wrong. | ||
""" | ||
nexus_client = workflow.create_nexus_client( | ||
service=MyServiceHandler, | ||
endpoint="fake-endpoint", | ||
) | ||
# assert-type-error-pyright: 'No overloads for "execute_operation" match' | ||
await nexus_client.execute_operation( # type: ignore | ||
MyServiceHandler.my_sync_operation, # type: ignore[arg-type] | ||
# assert-type-error-pyright: 'Argument of type .+ cannot be assigned to parameter "input"' | ||
"wrong-input-type", # type: ignore | ||
) | ||
|
||
|
||
@workflow.defn | ||
class MyWorkflow5: | ||
@workflow.run | ||
async def test_invoke_by_operation_handler_method_on_wrong_service(self) -> None: | ||
""" | ||
When a nexus client calls an operation by referencing an operation handler method | ||
on a service handler, there is a type error if the method does not belong to the | ||
service for which the client was created. | ||
|
||
(This form of type safety is not available when referencing an operation definition) | ||
""" | ||
nexus_client = workflow.create_nexus_client( | ||
service=MyServiceHandler, | ||
endpoint="fake-endpoint", | ||
) | ||
# assert-type-error-pyright: 'No overloads for "execute_operation" match' | ||
await nexus_client.execute_operation( # type: ignore | ||
# assert-type-error-pyright: 'Argument of type .+ cannot be assigned to parameter "operation"' | ||
MyServiceHandler2.my_sync_operation, # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type assertion failed without the fix in this PR |
||
MyInput(), | ||
) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR reveals one bug in Nexus typing: it should be a type error to reference an operation on a
ServiceHandler
other than the one that was used to instantiate the client but, before this fix, it was not.