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 inference bug. I’ll trace the existing variance machinery, add focused alias-chain and cycle coverage, implement the narrow fix, then run the prescribed tests and hooks.
- Assistant
The fault is localized: variance traversal explicitly returns bivariant for the lazy
Type::TypeAliasrepresentation, even though the runtimeTypeAliasTypeobject already traverses its raw value. I’m checking cycle-query patterns before changing that branch so recursive aliases retain the intended safe fallback. - Assistant
The focused test build is underway. I added coverage for all four variance kinds through a second alias layer, reordered two-parameter chains, and a PEP 695 alias passing through two legacy generic aliases; the implementation routes lazy aliases through a Salsa-tracked query with bivariant cycle recovery.
- Assistant
The first pass passes, including a mutually recursive case. One subtle recursion hazard remains: directly expanding a specialized recursive alias can create ever-growing keys such as
Alias[list[T]]. I’m tightening the implementation to infer the unspecialized alias once and compose that result with its specialization, matching the generic-class variance algorithm and ensuring Salsa’s cycle fallback always sees a stable key. - Assistant
The complete
ty_python_semanticsuite now passes: 203 unit tests, 14 corpus tests, 338 mdtests, and doctests. No pending snapshots appeared in the focused run; I’m doing the repository-required hook pass and final diff review now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →
- fable-5Compare side by side →