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 start by exploring the repository structure and locating the variance-tracking logic for type aliases.
- Assistant
This is the ruff repository with ty type checker. Let me search for variance-related code for type aliases.
- Assistant
rg is not available, let me use grep.
- Assistant
There's a variance.rs file. Let me look at that and also search for type alias variance specifically.
- Assistant
There's a type_alias_variance test. Let me look at the variance.rs and type_alias related files.
- Assistant
Let me look at type_alias.rs and the variance computation for type aliases.
- Assistant
Let me read the variance.rs and the known_instance variance for TypeAliasType.
- Assistant
Let me search for where variance of type aliases is computed.
- Assistant
So for TypeAliasType (the KnownInstanceType representing typing.TypeAliasType instance), variance goes through raw_value_type. But what about Type::TypeAlias?
- Assistant
Found it! Around line 6870, Type::TypeAlias(_) is treated somehow. Let me look at that variance_of implementation more carefully.
- Assistant
Let me see more of this match.
- Assistant
So
Type::TypeAlias(_)always returns Bivariant! That's the bug. Let me understand how type alias values are typed. - Assistant
Let me look at the existing type alias variance tests and how Class/GenericAlias handle nested aliases.
- Assistant
Let me also check how GenericAlias and NominalInstance handle variance — that should show the compose pattern.
- Assistant
Let me look at GenericAlias variance_of - that's the pattern we need for type aliases with specialization.
- Assistant
Let me check all TypeAlias variance paths and any related cycle handling.
- Assistant
Let me check what type expressions for type-alias subscripts resolve to, and look at legacy alias handling.
- Assistant
Let me look at subscript handling for TypeAliasType.
- Assistant
Looking at how type expressions resolve type aliases:
- Assistant
So when you use
RefBox[T]as a type expression RHS of another alias, it becomesType::TypeAlias(specialized_RefBox). And variance of Type::TypeAlias currently returns Bivariant always - THAT's the... - Assistant
Looking at how RHS of type aliases is inferred and whether it's
in_type_expression'd: - Assistant
Let me look at definition_expression_type
- Assistant
Looking at infer_type_alias:
- Assistant
Let me look at the VarianceInferable trait and cycle handling patterns.
- Assistant
Let me check legacy type alias handling and see if there are related md/tests.
- Assistant
Checking default_specialization and how applying specialization affects typevars:
- Assistant
Let me read those.
- Assistant
value_type uses default_specialization which uses specialize_partial with all None - which fills in defaults for typevars that have defaults, and typevars themselves for ones generation.
- Assistant
Let me look at fills_in_defaults to see what None becomes.
- Assistant
Hmm
- Assistant
typo - fill_in_defaults without s
- Assistant
I'll look at how similar tracked variance is implemented on FunctionType and TypeAlias for full picture:
- Assistant
Note that for FunctionType, variance is a method on the type, and VarianceInferable just calls it. Looking at how ClassType does it and VarianceInferable for function...
- Assistant
Checking is_specialized and how to structure the implementation:
- Assistant
Let me also look for any existing failing tests or TODO comments about this:
- Assistant
There's a variance section for type aliases in variance.md. Let me read that.
- Assistant
Let me read that section.
- Assistant
I have a clear plan. Implementing variance recursion through type alias values with cycle fallback.
- Assistant
Let me check what's after line 229 for TypeAliasType methods, and imports needed.
- Assistant
Implementing the fix now.
- Assistant
Need to open the file first at that line.
- Assistant
Let me try the replace again.
- Assistant
I'll add the variance method to PEP695TypeAliasType and update TypeAliasType.
- Assistant
I'll add variance_of after generic_context on PEP695, and on Manual, and on TypeAliasType. Thenimplement VarianceInferable for TypeAliasType.
- Assistant
Now add varianceamyl to TypeAliasType and Manual, then update Type::variance_of and KnownInstance.
- Assistant
Need to goto the right location.
- Assistant
Now I can use replace.
- Assistant
Now update Type::variance_of and KnownInstance.
- Assistant
I need to separate Type::TypeAlias from the bivariant group.
- Assistant
Now update KnownInstanceendp to use type_alias.variance_of instead of raw_value_type.
- Assistant
Update known_instance variance_of.
- Assistant
I need to check if BoundTypeVarInstance and VarianceInferable are properly exported from types module for type_alias.rs imports.
- Assistant
VarianceInferable is private (use not pub use). Type_alias.rs is under types:: so it can use crate::types::VarianceInferable since the use is available in the parent... wait, type_alias is a submodule, and VarianceInferable is imported in types.rs with
usenot re-exported. Submodules don't see parent's imports. - Assistant
I need to fix imports in type_alias.rs.
- Assistant
Let me fix the importsin type_alias.rs properly.
- Assistant
VarianceInferable is not pub re-exported from types. BoundTypeVarInstance - is it pub?
- Assistant
typevar is private mod. Let me check if BoundTypeVarInstance is re-exported.
- Assistant
BoundTypeVarInstance and TypeVarVariance are pub use. VarianceInferable is private. Fix import.
- Assistant
Now let me also update the unit test to cover nested aliases, and run tests. First, let me update the existing type_alias_variance test and extend variance.md.
- Assistant
private clay mod variance - but sibling modules can access crate::types::varianceà. Good.
Now let me expand the unit test and then try to compile/run tests.
- Assistant
The instructions say not to modify existing tests. Let me compile and run the existing variance tests.
- Assistant
The existing test passes. Now let me write a temporary test to verify nested aliases.
- Assistant
I accidentally introduced a typo check - let me see and run the test. Also I need to fix order of type aliases - NestedInvariantAlias relies. on InvariantAlias which is defined before, ok.
- Assistant
All nested variance tests pass. Reverting the temporary test modifications.
- Assistant
Good. Now let me run the variance mdtests to make sure nothing broke.
- Assistant
Different test runner syntax.
- Assistant
There's a legacy variance.md geld too. Good both pass. Let me also verify_ f the original issue via a temporary mdtest.
- Assistant
I had garbage characters in the test file. Let me fix.
- Assistant
All temporary tests pass. Removing the temporary test directory.
- Assistant
Let me also run aliases.md trăm and type_alias_variance once more to be sure.
- Assistant
The filter with | might not work. Let me run separately.
- Assistant
Everything looks good. Let me review the final diff one more time.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.