Skip to content

proto: pin the v6 arm for type checkers so proto symbols are not a v5 | v6 union - #228

Open
deanrie wants to merge 1 commit into
xai-org:mainfrom
deanrie:typing/proto-type-checking-arm
Open

deanrie wants to merge 1 commit into
xai-org:mainfrom
deanrie:typing/proto-type-checking-arm

Conversation

@deanrie

@deanrie deanrie commented Oct 16, 2026

Copy link
Copy Markdown

Summary

Fixes #177. src/xai_sdk/proto/__init__.py chooses .v5 or .v6 at runtime from google.protobuf.__version__. Type checkers cannot evaluate that gate, so they keep both arms live and type every re-exported *_pb2 symbol as v5.X | v6.X. Because the SDK's public API is annotated with these types, the union spreads into every consumer; @dsfaccini measured 45 spurious invalid-argument-type diagnostics under ty in pydantic-ai's xai model from this alone, and it cannot be fixed from the consumer side.

This adds an if TYPE_CHECKING: arm that imports the .v6 modules and turns the runtime if into elif. The v5 and v6 generated modules expose the same classes, so pinning one arm for typing is accurate. Runtime behaviour is unchanged: the elif chain is what executes.

The change is the one proposed in #177; opening it as a PR so it can be reviewed, with credit to @dsfaccini for the analysis.

Verification

… | v6 union

Static checkers cannot evaluate the runtime protobuf-version gate, so they
kept both import arms live and typed every proto symbol as a v5 | v6
union. Because the SDK's public API is annotated with these types, the
union spread into downstream code (45 spurious invalid-argument-type
diagnostics under ty in pydantic-ai's xai model). Add a TYPE_CHECKING arm
that imports the v6 modules; the elif chain that runs at runtime is
unchanged.

Fixes xai-org#177.
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.

[FEATURE] Make version-gated proto imports statically analyzable (src/xai_sdk/proto/__init__.py)

1 participant