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
from 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 working-on-ty skill because this is a ty semantic variance-inference bug. I’ll trace the alias variance path, add focused regression coverage for modern and legacy aliases (including cycles), then run the narrow tests and repository hooks.
- Assistant
The failure is at the alias boundary: specialized alias values are represented as
Type::TypeAlias, and variance traversal currently labels that variant bivariant instead of following its value. I’m adding cycle-aware traversal at that boundary so chains preserve parameter mapping and recursive aliases still converge to the existing bivariant fallback. - Assistant
The focused Rust test now passes, including two-deep chains, all four variance kinds, reordered four-parameter mappings, and mutual cycles. I’m adding an end-to-end typing test for both PEP 695 and legacy generic alias chains so the externally observable subtype behavior is locked down as well.
- Assistant
Both end-to-end alias styles pass. The required repository hook runner isn’t installed in this environment (
uvx: command not found), so I’m substituting the relevant local formatting and lint checks, then I’ll rerun the focused tests after any formatting changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.