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’m using the repository’s working-on-ty skill because this is a ty variance-inference bug. I’ll trace the alias variance logic, add focused regression coverage for modern and legacy aliases, then run the targeted tests and hooks.
- Assistant
The failure is localized: variance traversal expands a type alias’s RHS once, but the resulting
Type::TypeAliasnode is currently hard-coded as bivariant. I’m checking the existing Salsa cycle patterns before making that branch recursively query the alias safely. - Assistant
The recursive query now compiles with the repository’s standard bivariant cycle seed. I’ve extended the low-level test to cover three-deep aliases, every variance kind, reordered two-parameter chains, and mutual recursion; the targeted test binary is still linking.
- Assistant
Both PEP 695 and legacy behavioral regressions now pass. I also applied the same tracked cycle guard to manually constructed
TypeAliasTypevalues, so making alias traversal recursive does not introduce an unguarded recursion path outsidetypestatements. - Assistant
The full semantic crate unit suite passes (203 passed, 34 ignored), along with both focused mdtest files. The required repository hook cannot run in this environment because neither
uvx/preknor Rust’s formatting component is installed; I’m doing the remaining diff and artifact checks directly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.