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 start by exploring the repository structure and locating the variance-tracking logic for type aliases.
- Assistant
Next I'll search the codebase for variance inference on type aliases.
- Assistant
Found variance.rs and related test files. Let me look at them.
- Assistant
Let me search for where type alias variance is computed.
- Assistant
Let me look at the variance computation for type aliases more carefully.
- Assistant
Let me look at type_alias module and VarianceInferable for TypeAlias
- Assistant
Let me look at VarianceInferable for Type and KnownInstanceType
- Assistant
Let me see the rest of the variance_of function for Type, especially how TypeAlias is handled.
- Assistant
Found it —
Type::TypeAlias(_)is treated as Bivariant. I'll check how alias specialization/variance is computed next. - Assistant
Let me look at KnownInstance variance and how alias specialization is applied.
- Assistant
I'll inspect the type_alias module structure and how cycles are handled.
- Assistant
I'll also check how legacy type aliases handle variance and look at related tests.
- Assistant
Let me look at the type aliases section in pep695 variance.md
- Assistant
The existing tests only go one level deep for type aliases. The bug is about two levels deep.
- Assistant
Let me look at how specialization relations work for type aliases - lines 2366 and 2896.
- Assistant
Now I understand the fix. Let me look at how VarianceInferable trait is defined and implement for TypeAliasType.
- Assistant
Checking how subscripting a type alias is represented.
- Assistant
Let me find Infer_explicit_type_alias_type_specialization
- Assistant
Hmm, path issue with grep.
- Assistant
Let me search for the function definition
- Assistant
Let me look at that function.
- Assistant
Found a likely bug: specialized aliases use
raw_value_typeinstead ofvalue_type. Checking next. - Assistant
Let me look at default and identity specializations.
- Assistant
I'll also confirm how legacy type aliases are represented.
- Assistant
I'll implement
VarianceInferablefor type aliases and update the call sites. - Assistant
Let me also look at how TypeVar get variance when used as class - BoundTypeVarInstance and how variance is used for type alias subtyping.
- Assistant
When typevar variance is inferred for a type alias definition:
- Assistant
Implementing the fix: adding
VarianceInferablefor type aliases and updating call sites. - Assistant
I'll implement
VarianceInferableand update the relevant call sites. - Assistant
Next I'll add the VarianceInferable implementations.
- Assistant
I'll insert VarianceInferable impls after the PEP695 impl and after Manual impl, and for TypeAliasType.
- Assistant
Now add Manual and TypeAliasType variance impls after Manual's impl.
- Assistant
Now add TypeAliasType VarianceInferable after its methods impl.
- Assistant
Add TypeAliasType VarianceInferable after line 341.
- Assistant
Now update KnownInstance and Type::TypeAlias
- Assistant
Need to open the file first or goto that line.
- Assistant
Now replace.
- Assistant
Now update Type::TypeAlias in types.rs
- Assistant
I need to handle Type::TypeAlias separately from the Bivariant group.
- Assistant
I'll add a nested-alias unit test, then compile and run the variance tests.
- Assistant
I won't add permanent tests. Compiling and running existing variance tests next.
- Assistant
Unit test passed. Next I'll verify nested aliases with a temporary reproduction.
- Assistant
I'll try verifying with the ty CLI and a temporary script.
- Assistant
Let me create a temp Python file that reproduces the issue using is_subtype_of assertions.
- Assistant
Need to specify Python 3.12+
- Assistant
All checks passed. Let me also verify the transmute case from the issue and run the existing variance mdtests.
- Assistant
I'll run broader related tests and check for compile warnings.
- Assistant
I'll confirm BoundTypeVarInstance is exported and check for unused-import warnings.
- Assistant
Testing multi-parameter variance reordering next.
- Assistant
I left garbled text in the command. Let me write a clean file.
- Assistant
Let's also test mutual recursive aliases:
- Assistant
Reviewing the final diff before submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.