ISSUE DESCRIPTION Type alias statements stop tracking typevar variance after two deep…
- User
ISSUE DESCRIPTION
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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- Assistant
I’ll trace variance inference for type aliases, identify the existing cycle guard and tests, then implement recursive alias-value traversal for both PEP 695 and legacy aliases. I’ll run focused tests plus relevant broader tests, and inspect the final diff for edge cases such as parameter reordering and recursive cycles.
- Assistant
The relevant paths are clear: specialized aliases are represented as
KnownInstanceType::TypeAliasType, whose variance implementation currently inspectsraw_value_type, bypassing the alias specialization; lazily unpacked aliases are also explicitly treated as bivariant inType::variance_of. I’ll make both paths recurse through the applied alias value. Alias value evaluation already has Salsa cycle normalization toDivergent, whose variance is safely bivariant, preserving the requested cycle fallback. - Assistant
The implementation now has two complementary fixes:
Type::TypeAliasdelegates variance to its value, andKnownInstanceType::TypeAliasTypeusesvalue_type(including specialization mappings) instead ofraw_value_type. This handles nested aliases and reordered parameters while retaining existing cycle normalization. I’m doing the available static validation now; the repository’s configured Rust toolchain is incomplete, so compilation may be unavailable in this environment. - Assistant
Static validation passed:
rustfmt --checkandgit diff --checkreport no issues. The only unavailable validation is the Rust test suite because the configured1.96toolchain installation is missing its manifest, so Cargo cannot start. The final diff is limited to the two variance dispatch points and does not alter tests or interfaces.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.