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.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /ruff, current file: none) bash-$
- Assistant
I'll start by exploring the repository structure and locating the variance-tracking logic for type aliases.
- Assistant
Searching for variance inference code related to type aliases.
- Assistant
rg is not available, let me use grep.
- Assistant
Let me look at the variance.rs file and type alias related code.
- Assistant
Reading the type alias variance tests and variance implementation.
- Assistant
Let me look at how variance is computed for generic contexts and type aliases.
- Assistant
Found a likely culprit: TypeAlias appears to always return bivariant. Reading the full
variance_ofimplementation. - Assistant
Let me see lines around 6870 more carefully.
- Assistant
Checking how KnownInstance and specialized aliases compute variance, and reviewing related tests.
- Assistant
Inspecting TypeAliasType value methods and how specialized aliases are represented.
- Assistant
Checking variance for recursive aliases and cycle handling.
- Assistant
Checking how cycle recovery works for variance computation.
- Assistant
I need to understand how nested type-alias references appear in the type graph and whether cycle detection is required.
- Assistant
Reading the variance and alias mdtests for expected behavior.
- Assistant
Checking how specialized type aliases appear when nested, and inspecting the NewType path.
- Assistant
Checking legacy type alias variance handling.
- Assistant
Legacy type aliases via TypeAlias annotation are actually just type assignments - the type gets stored as the RHS type directly rather than Type::TypeAlias. Looking at:
- Assistant
Let me read the type aliases section of pep695 variance.md fully.
- Assistant
How does Type::TypeAlias participate in subtype relations?
- Assistant
Checking how typevar variance is computed for type aliases.
- Assistant
Checking default specialization and whether salsa tracked methods can be on enums.
- Assistant
I'll implement VarianceInferable for PEP695TypeAliasType and ManualPEP695TypeAliasType with cycle_initial Bivariant, andWIRE Type::TypeAlias and KnownInstance to use them.
- Assistant
Checking how subtyping of specialized type aliases uses variance of the alias's typevars.
- Assistant
Checking how specialization applies to nested Type::TypeAlias values.
- Assistant
Looking for contextroom around issue #3517 and any incomplete performance.
- Assistant
Implementing VarianceInferable on type aliases with cycle-safe recursion.
- Assistant
Implementing the fix now.
- Assistant
Now I need to find where PEP695 methods end and add VarianceInferable impl.
- Assistant
I'll add VarianceInferable after the PEP695 block (after line 148) and after ManualPEP695 block.
- Assistant
Now add VarianceInferable for Manual and TypeAliasType enum.
- Assistant
Now add VarianceInferable for TypeAliasType enum. I'll add it after the enum互换 impl block.
- Assistant
I'll add VarianceInferable for TypeAliasType after the TypeAliasType impl.
- Assistant
Now update Type::TypeAlias variance and KnownInstance.
- Assistant
Now update KnownInstance to use type_alias.variance_of.
- Assistant
Oops, I left an extra
}. Fixing that. - Assistant
Need to check if BoundTypeVarInstance and VarianceInferable are imported ink known_instance.rs.
- Assistant
Checking that VarianceInferable is in scopein types.rs:
- Assistant
Extending the unit test for nested alias variance, then compiling and testing.
- Assistant
I need to add the assertions for nested aliases. Also extend get_bound_typevar to support multi-parameter if needed.
- Assistant
I need to update all the get_bound_typevar calls to pass index 0, and add nested assertions.
- Assistant
I shouldn't modify existing tests. Restoring the test file and using a temporary script instead.
- Assistant
Compiling and running the varianceand type-alias tests.
- Assistant
Existing unit test passed. Writing a temporary test for nested aliases.
- Assistant
I'll adda temporary test and then remove it. Or better - create tempfile mdtestadaptive.
- Assistant
Need BoundTypeVarInstance importin tests. Checking the imports top of tests.rs.
- Assistant
super::* should include BoundTypeVarInstance. Let me run the test.
- Assistant
Nested cases pass. Removing the temp test and running the variance mdtests.
- Assistant
All variance tests pass. Reproducing the issue via a temporary mdtest.
- Assistant
Tidying up the temporary mdtest and verifying the finaldiff.
- Assistant
Running a broader regression around aliases and cycles.
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
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →