Skip to content

Conversation

@dandavison
Copy link
Contributor

@dandavison dandavison commented Jul 12, 2025

This is PR 4/5 in a stack.

Introduces nexusrpc.OperationDefinition with non-nullable fields. This is the internal counterpart of the user-facing syntactic sugar nexusrpc.Operation.

It also performs a number of variable renames to avoid confusion between the two classes. This PR is pure refactoring without user-facing consequences.

@dandavison dandavison force-pushed the operation-definition branch 2 times, most recently from cde4a79 to 4eaba19 Compare July 12, 2025 21:12
@dandavison dandavison force-pushed the dataclass-transform branch from 4e78ff3 to f44be30 Compare July 12, 2025 21:31
@dandavison dandavison force-pushed the operation-definition branch from 4eaba19 to 8f114f8 Compare July 12, 2025 21:32
@dandavison dandavison force-pushed the dataclass-transform branch from f44be30 to 47219c7 Compare July 12, 2025 21:45
@dandavison dandavison force-pushed the operation-definition branch from 8f114f8 to 1189fb3 Compare July 12, 2025 21:45
@dandavison dandavison force-pushed the dataclass-transform branch from 47219c7 to 4af2252 Compare July 12, 2025 21:57
@dandavison dandavison force-pushed the operation-definition branch from 1189fb3 to 7cf8382 Compare July 12, 2025 21:57
@dandavison dandavison force-pushed the dataclass-transform branch from 4af2252 to d461e2c Compare July 12, 2025 22:08
@dandavison dandavison force-pushed the operation-definition branch from 7cf8382 to a888dda Compare July 12, 2025 22:08
@dandavison dandavison force-pushed the dataclass-transform branch from d461e2c to 48b5b5c Compare July 12, 2025 22:28
@dandavison dandavison force-pushed the operation-definition branch from a888dda to a8cd3e8 Compare July 12, 2025 22:28
@dandavison dandavison force-pushed the dataclass-transform branch from 48b5b5c to b65bae0 Compare July 13, 2025 16:25
@dandavison dandavison force-pushed the operation-definition branch from a8cd3e8 to 70e857c Compare July 13, 2025 16:25
@dandavison dandavison force-pushed the dataclass-transform branch from b65bae0 to 27110ec Compare July 13, 2025 18:50
@dandavison dandavison force-pushed the operation-definition branch 2 times, most recently from 97078d3 to 4fa4f0e Compare July 14, 2025 03:58
@dandavison dandavison force-pushed the dataclass-transform branch from 27110ec to 4cd141d Compare July 14, 2025 13:04
@dandavison dandavison force-pushed the operation-definition branch from 4fa4f0e to 79c228f Compare July 14, 2025 13:04
@dandavison dandavison changed the title [4/4] Introduce OperationDefinition [4/5] Introduce OperationDefinition Jul 14, 2025


def get_operation_definition(
def get_operation(
Copy link

@cretz cretz Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a user-facing use case for this or set_operation? I see we expose this call at the module level to users. I think we'd expect them to operate on a service definition and only we would be the ones translating and setting attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only known user of this library at the moment is Temporal (i.e. us, in another role). Temporal are adding additional decorators analogous to sync_operation.

Temporal use set_operation here to "officially" attach the nexusrpc.Operation to the user's operation handler method
https://github.com/temporalio/sdk-python/blob/nexusrpc-update/temporalio/nexus/_decorators.py#L120.

It seems to me that set_operation is an API that the library must expose, for this reason.

Their nexus caller uses get_operation in two situations, both in the situation where their caller invokes an operation by referring directly to the operation handler method (as opposed to the operation definition in the service definition):

  1. They need to obtain the operation name used to address the operation (i.e., perhaps the name override from the decorator): https://github.com/temporalio/sdk-python/blob/nexusrpc-update/temporalio/worker/_interceptor.py#L328
  2. They also use it to get the output type from the operation https://github.com/temporalio/sdk-python/blob/nexusrpc-update/temporalio/worker/_interceptor.py#L309-L310

Copy link

@cretz cretz Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that set_operation is an API that the library must expose, for this reason.

Hrmm, can end users' (and Temporal's) custom decorators not use the Nexus decorators themselves to mark this?

Their nexus caller uses get_operation in two situations, both in the situation where their caller invokes an operation by referring directly to the operation handler method (as opposed to the operation definition in the service definition)

Hrmm, arguably you should be able to obtain the service definition from the parent of the operation handler method in use. As a user that might want to write something that accepts either an operation contract or an operation handler, can I get the service definition that is on if I need/want it? Can Temporal do the same? Or maybe there's a static call on OperationDefinition to obtain just the operation if they don't care about which service it is on?

Also, why might a user want the nexusrpc.Operation (sugar for the definition) instead of the definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are good questions. Let's merge this as-is since it's forward progress and is ntot changing the public API?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add it to your list so we make sure we don't forget about it? Having these get/set functions as public API at the top-level Nexus module is a bit surprising (not to mention they seem to be the only non-decorator module functions we have in public Nexus API).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to list

@dandavison dandavison force-pushed the dataclass-transform branch from 4cd141d to 32318d5 Compare July 14, 2025 15:02
@dandavison dandavison force-pushed the operation-definition branch from 79c228f to 562a2fd Compare July 14, 2025 15:02
@dandavison dandavison force-pushed the dataclass-transform branch from 32318d5 to 02397dd Compare July 14, 2025 15:08
@dandavison dandavison force-pushed the operation-definition branch from 562a2fd to 36da5d3 Compare July 14, 2025 15:08
@dandavison dandavison force-pushed the dataclass-transform branch from 02397dd to cb4777e Compare July 14, 2025 15:56
@dandavison dandavison force-pushed the operation-definition branch from 36da5d3 to 9db6a66 Compare July 14, 2025 15:56
AI: This commit should have tag name type-assertions and should be
force-pushed to a branch of that name as necessary.
AI: This commit should have tag name dataclass-transform and should be
force-pushed to a branch of that name as necessary.
These are only required if the service handler does not specify an
service definition. In that case, there will be an error when
attempting to sythesize a valid OperationDefinition for the operation,
since input and output types are non-nullable on OperationDefinition.

AI: This commit should have tag name operation-definition and should be
force-pushed to a branch of that name as necessary.
@dandavison dandavison force-pushed the dataclass-transform branch from cb4777e to 9c06a2d Compare July 14, 2025 17:42
@dandavison dandavison force-pushed the operation-definition branch from 9db6a66 to b6a3d13 Compare July 14, 2025 17:42
Base automatically changed from dataclass-transform to main July 14, 2025 19:13
@dandavison dandavison merged commit 8387369 into main Jul 14, 2025
10 checks passed
@dandavison dandavison deleted the operation-definition branch July 14, 2025 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants