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
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.
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:
- Assistant
I’ll trace variance inference for type aliases, identify the existing cycle handling and tests, then implement recursive alias-value propagation for both PEP 695 and legacy aliases. I’ll run focused tests (and broader relevant tests if practical), inspect the diff, and only then submit.
- Assistant
rgis unavailable in this environment, so I’ll usegrep/findinstead while keeping the investigation focused on the Rust ty analyzer sources. - Assistant
The relevant paths confirm the defect:
Type::TypeAliasis explicitly grouped with unconditional bivariant cases, andKnownInstanceType::TypeAliasTypecurrently inspects the raw RHS, which skips specialization when an alias is nested in another alias. I’ll change both to recurse through the specialized value type. The existing Salsa cycle recovery on the RHS yields a divergent type, whose variance is already safely bivariant, preserving the requested cycle fallback. - Assistant
The focused unit test initially exposed why blindly using
value_typeeverywhere would regress declaration-level inference: applying a default specialization erases the alias’s own type variables. I corrected this by preserving raw-RHS analysis for an alias declaration, while expandingvalue_typeonly for specialized nested PEP 695 aliases. Both PEP 695 and legacy variance suites now pass, along with formatting and all focused type tests. I’ll do the final status check and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.