Skip to content

fix: import temporalio.client only for type checking in a generated Python client - #115

Closed
moetemp wants to merge 4 commits into
moe/source-fallbackfrom
moe/client-import-cycle
Closed

moetemp wants to merge 4 commits into
moe/source-fallbackfrom
moe/client-import-cycle

Conversation

@moetemp

@moetemp moetemp commented Nov 13, 2026

Copy link
Copy Markdown
Owner

This PR makes a generated Python client module import temporalio.client only for type checking.

An SDK makes its Client class inherit the generated client extension. So temporalio.client
imports the generated package while it is being imported itself, and the generated modules
imported temporalio.client back at module level. In sdk-python, import <generated>.client
before import temporalio.client then failed with a circular import error.

The generated modules use temporalio.client in annotations only, and each one starts with
from __future__ import annotations. So the import moves under typing.TYPE_CHECKING.

if typing.TYPE_CHECKING:
    import temporalio.client

Only the client-target Python outputs change, and only in their import lines.

Notes for the review.

  • The rule covers every generated Python module that names temporalio.client. A later use at
    run time would need a plain import again.
  • The sample SDK's temporalio.client doesn't import the generated package, so the new test
    checks that a generated client package loads with no temporalio.client in sys.modules. I
    also regenerated the Stream bindings in sdk-python with this change, and the import order that
    failed there works.

The SDK's `Client` inherits the generated extension, so `temporalio.client` imports the generated
package. A run-time import back made importing the package first a circular import.

Co-authored-by: Tim Conley <timothy.conley@temporal.io>
The import cycle fix needs the same sample that the source fallback and the sync produce.
The import cycle fix builds on the source fallback. So it takes the `sequence` of `append` too.
The import cycle fix builds on the source fallback. So it takes the new sample comments too.
@moetemp

moetemp commented Nov 13, 2026

Copy link
Copy Markdown
Owner Author

Replaced by temporalio#409.

@moetemp moetemp closed this Nov 13, 2026
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.

1 participant