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 where alias values are short-circuited, then implement recursive substitution-aware inference with cycle protection. I’ll run focused tests (and add no repository tests), use temporary reproduction scripts if useful, and inspect the final diff before submission.
- Assistant
The relevant path is
KnownInstanceType::variance_of: forTypeAliasTypeit currently analyzesraw_value_type, which discards the alias specialization mapping. A nested alias therefore sees the inner alias’s type variable rather than the outer variable. I’ll switch this tovalue_type, which applies each alias’s specialization, while retaining the existing Salsa cycle fallback on value computation. - Assistant
The first direct
value_typechange exposed an important edge case: unspecialized aliases use a default specialization ofunknown, and recursively expanding an unspecialized cycle can overflow. The refined implementation keeps the raw value when the queried variable is owned by that alias (or for non-generic aliases), and only expands a generic alias when the variable comes from an enclosing alias. This preserves cycle normalization while propagating mappings through nested aliases. I’ll now format-check and run the broader semantic type tests.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.