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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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?

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