Skip to content

Python: preserve PEP 695 type-alias type parameters across RPC#8331

Open
shanman190 wants to merge 2 commits into
mainfrom
python-typealias-type-parameters
Open

Python: preserve PEP 695 type-alias type parameters across RPC#8331
shanman190 wants to merge 2 commits into
mainfrom
python-typealias-type-parameters

Conversation

@shanman190

@shanman190 shanman190 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Py.TypeAlias had no typeParameters field on the Java side, so all four RPC codecs (Python + Java, sender + receiver) dropped it. type X[T] = ... was silently narrowed to type X = ... once the tree crossed the RPC boundary — a print-fidelity loss invisible to the in-process printer suite and baked into every serialized LST.

Change

  • Py.TypeAlias — add @Nullable JContainer<J.TypeParameter> typeParameters (mirroring J.ClassDeclaration), with the unwrapped getter, withTypeParameters, and Padding accessors.
  • All four codecsPythonSender/PythonReceiver.visitTypeAlias and _visit_type_alias on both Python sides send/receive the container between name and value.

Both serialization formats (V2 Jackson-reflection, V3 reflective field walk) discover the new field automatically, so no moderne-cli change is needed.

Verification

  • Python RPC round-trip test (tests/rpc/test_type_alias_roundtrip.py) and a Java PythonSenderReceiverRoundTripTest case cover simple, bound (T: int), variadic (*Ts) and ParamSpec (**P) parameters, plus the no-params regression.

  • Serialize→deserialize→print corpus: numpy 3 → 0 and home-assistant 15 → 0 TypeAlias round-trip mismatches; existing type-alias parser/printer suite green.

  • Draft: independent of the match-parser fix (Python: fix match-statement parse desync that corrupts trees over RPC #8330). Optional follow-up: a Java assertion for the null (no-params) path — currently covered by the getAndSend null-guard and the Python test.

Py.TypeAlias had no typeParameters field on the Java side, so all four RPC
codecs (Python + Java, sender + receiver) dropped it. `type X[T] = ...` was
silently narrowed to `type X = ...` once the tree crossed the RPC boundary —
a print-fidelity loss invisible to the in-process printer suite and baked into
every serialized LST.

Add the field to Py.TypeAlias (JContainer<J.TypeParameter>, mirroring
J.ClassDeclaration) and send/receive it between name and value in all four
codecs. Both serialization formats (V2 Jackson-reflection, V3 reflective field
walk) discover the new field automatically, so no moderne-cli change is needed.

Verified: Python and Java RPC round-trip tests preserve simple, bound, variadic
and ParamSpec parameters; numpy (3 -> 0) and home-assistant (15 -> 0) TypeAlias
round-trip mismatches resolved; existing type-alias parser/printer suite green.
@shanman190 shanman190 self-assigned this Jul 26, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 26, 2026
Space out the type parameters (before `[`, inside the brackets, around the `:`
bound and the commas) so the round-trip also asserts JContainer.before and the
element padding survive, not just the parameters' presence.
@shanman190
shanman190 marked this pull request as ready for review July 26, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant