Skip to content

Fix circular import: do not import temporalio.client in temporalio.nexus #973

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 1 commit into from
Aug 16, 2025

Conversation

dandavison
Copy link
Contributor

On main, a circular import error is revealed by doing e.g.

uv run tests/nexus/test_type_checking.py


Traceback (most recent call last):
  File "/Users/dan/src/temporalio/sdk-python/tests/nexus/test_type_checking.py", line 3, in <module>
    import temporalio.nexus
  File "/Users/dan/src/temporalio/sdk-python/temporalio/nexus/__init__.py", line 9, in <module>
    from ._decorators import workflow_run_operation as workflow_run_operation
  File "/Users/dan/src/temporalio/sdk-python/temporalio/nexus/_decorators.py", line 19, in <module>
    from temporalio.nexus._operation_context import (
  File "/Users/dan/src/temporalio/sdk-python/temporalio/nexus/_operation_context.py", line 22, in <module>
    import temporalio.client
  File "/Users/dan/src/temporalio/sdk-python/temporalio/client.py", line 60, in <module>
    import temporalio.workflow
  File "/Users/dan/src/temporalio/sdk-python/temporalio/workflow.py", line 62, in <module>
    from temporalio.nexus._util import ServiceHandlerT
  File "/Users/dan/src/temporalio/sdk-python/temporalio/nexus/_util.py", line 21, in <module>
    from temporalio.nexus._operation_context import WorkflowRunOperationContext
ImportError: cannot import name 'WorkflowRunOperationContext' from partially initialized module 'temporalio.nexus._operation_context' (most likely due to a circular import) (/Users/dan/src/temporalio/sdk-python/temporalio/nexus/_operation_context.py)

While that's not a command a user would actually run, the situation it reflects is undesirable and this will bite us at some point: temporalio.nexus has a run-time import dependency on temporalio.client.

This PR removes the dependency.

@dandavison dandavison requested a review from a team as a code owner July 16, 2025 12:30
@dandavison dandavison force-pushed the pr-971-circular-import branch from edcd5b9 to 291687b Compare July 16, 2025 14:22
@@ -32,6 +32,9 @@
SelfType,
)

if TYPE_CHECKING:
import temporalio.client
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is client still needed in type checking if the callback has moved here? Is there another thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are methods in the nexus module that return types from temporalio.client. The PR makes it so there is no run-time dependency from nexus -> temporalio.client.

@dandavison dandavison force-pushed the pr-971-circular-import branch from 291687b to 8a34d99 Compare July 28, 2025 18:36
@dandavison dandavison force-pushed the pr-971-circular-import branch from 8a34d99 to 25833a0 Compare August 15, 2025 22:19
@dandavison dandavison force-pushed the pr-971-circular-import branch from 25833a0 to fcb6217 Compare August 15, 2025 23:32
@dandavison dandavison merged commit 49c2400 into main Aug 16, 2025
28 of 29 checks passed
@dandavison dandavison deleted the pr-971-circular-import branch August 16, 2025 13:32
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