Type alias statements stop tracking typevar variance after two deep Summary Ran into…
- User
Type alias statements stop tracking typevar variance after two deep
Summary
Ran into this while making my umpteenth generic parser generator soup and messed up a parser generator transformer. https://play.ty.dev/d0fab0ef-bf5d-496f-b305-046e604cd702
pyfrom dataclasses import dataclass @dataclass class Box[T]: # T is invariant item: T type RefBox[T] = Box[T] # T is invariant type RefRefBox[T] = RefBox[T] # T is bivariant???When combined with #3517, you can make a type safe transmute (this happened in my actual code): https://play.ty.dev/eff23d78-ef4e-4bda-8aa7-b9848e527cb3
Version
ty 0.0.35 (bc12d1c 2026-05-10) playground 7efc9a615
Evaluation contract
Variance inference for both PEP 695 and legacy generic type aliases must recurse through the alias value rather than treating every alias as bivariant. Preserve covariant, contravariant, invariant, and bivariant results through multi-level and reordered multi-parameter alias chains. Recursive or mutually recursive alias cycles must terminate safely using the existing bivariant cycle fallback.
Interface
No new interfaces are introduced.
- Assistant
I’ll trace ty’s alias variance inference and its existing cycle handling, then add focused mdtests for deep, reordered, legacy, and recursive aliases. I’m using the working-on-ty skill because this is a ty semantic fix.
- Assistant
The checkout is clean.
rgisn’t installed here, so I’m switching to targetedgrep/findsearches and keeping the investigation scoped to ty’s semantic crate. - Assistant
The failure is at
Type::TypeAlias: variance traversal deliberately returns bivariant there, so the first alias level works through the alias object’s raw RHS, but the next level stops. I’m moving alias-value traversal behind a Salsa-tracked query so deep chains recurse while self/mutual cycles retain the existing bivariant fallback. - Assistant
The tracked recursion compiles; the first targeted test build is still linking the large semantic test binary. Once it finishes, I’ll extend the existing variance tests rather than create a new test file, per the repository guidelines.
- Assistant
The focused Rust test now covers two-level aliases for all four variances, a two-level four-parameter reordered chain, direct recursion, and mutual recursion. I’ve also extended the existing legacy-alias mdtest to exercise deep and reordered chains through generic base validation.
- Assistant
The full
ty_python_semanticsuite passes: 203 unit tests, 14 corpus tests, 338 mdtests, and doctests. No snapshots or pending snapshots were created. I’m at the repository-required hook pass now, covering exactly the six changed files. - Assistant
The required hook launcher can’t run because
uvxis not installed in this environment. I’ll still run the equivalent local formatting checks available in the checkout, then re-run the affected tests if formatting changes anything. - Assistant
Formatting and whitespace checks pass. The environment has the Clippy binary but its Rustup component registration is broken in the same way as rustfmt; I’m invoking the installed tool directly for the targeted crate check.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.