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
Let me start by understanding the repository structure and the issue. The issue is about type alias statements not properly tracking TypeVar variance after two levels of nesting. Let me explore the repository first.
This appears to be a type checker written in Rust (based on the "ruff" name and directory). Let me look around.
Actually, looking at the issue description, it mentions "ty 0.0.35" and the playground link suggests it might be related to the "ty" type checker. But the repository root is
/ruff. Let me explore.Assistant
Let me explore the crate structure to find the type checker code.
Assistant
Let me search for variance-related code and type alias handling in the
tycratesAssistant
Let me look at the key files. Let me first search for "TypeAlias" or "type_alias" to understand how type aliases are handled.
Assistant
Let me look at the
type_aliasmodule and the variance computationAssistant
Let me look at the variance computation. Let me first look at the type_alias.rs file.
Assistant
Let me open the file.
Assistant
Let me use the correct format - the open command requires the file path.
Assistant
Let me see the rest of the file and particularly the variance computation.
Assistant
Let me search for variance computation in the codebase
Assistant
Let me look at the variance computation in
known_instance.rs- this seems to be the key implementation.Assistant
Let me use the correct syntax.
Assistant
Let me look at the VarianceInferable implementation.
Assistant
Let me see the full implementation of variance_of for KnownInstanceType.
Assistant
Let me look at how TypeAliasType is defined.
Assistant
Let me fix the grep.
Assistant
Let me look at the TypeAliasType enum
Assistant
Let me look at line 204 in type_alias.rs.
Assistant
Let me see the rest of the file
Assistant
Let me look at the test for type alias variance to understand the expected
Assistant
Let me look at the test
Assistant
Let me look at how variance is computed for class types with specialization:
Assistant
Let me use the open command properly
Assistant
Let me look at how
definition_expression_typehandles type alias references with specialization.Assistant
Let me look at the definition_expression_type function
Assistant
Let me look at how Type represents type alias types with specialization
Assistant
Let me look at Type::TypeAlias usage
Assistant
Let me check the variance_of implementation for Type
Assistant
Let me continue viewing the variance_of implementation
Assistant
Let me look at where Type::TypeAlias is constructed vs Type::KnownInstance TypeAliasType
Assistant
Let me find where Type::TypeAlias is constructed
Assistant
Let me search for where KnownInstanceType::TypeAliasType is constructed
Assistant
Let me search
Assistant
Let me check where KnownInstanceType::TypeAliasType is constructed with specialization
Assistant
Let me look at how type alias subscript is handled
Assistant
Let me find the
infer_explicit_type_alias_type_specializationfunctionAssistant
Let me look at this function
Assistant
Let me first create a reproduce script to confirm the bug
Assistant
Let me find the test file and see if there are existing tests I can extend or if I can run them.
Assistant
Let me look at the Specialization struct to check its methods
Assistant
Let me look at the Specialization type
Assistant
Let me look at the Specialization struct and its methods
Assistant
Now let me look at the GenericContext to understand its variables method
Assistant
Let me see the variables method on GenericContext
Assistant
Let me understand the
with_polarityandcomposemethodsAssistant
Let me look at
composeandcompose_thunkfor TypeVarVarianceAssistant
Let me look at the compose method
Assistant
Let me look at line 5981 in types.rs where Type::TypeAlias is
Assistant
Now I understand the issue. Let me implement the fix. The problem is in
KnownInstanceType::variance_of- when a type alias has a specialization, it should walk through the specialization parameters to compose variances, similar toGenericAlias::variance_of. Let me edit the fileAssistant
Now let me run the existing test to make sure the change compiles and passes
Assistant
Now let me also fix the
Type::TypeAliascase in types.rs. Let me look at that sectionAssistant
Let me move
Type::TypeAliasout of the bivariant catch-all and handle it properlyAssistant
Let me fix the
Type::TypeAliashandling differently. Let me replace just that line:Assistant
Let me open the file first.
Assistant
Now let me replace the line
Assistant
Now let me verify that the code compiles
Assistant
Good, it compiles! Let me run the existing tests again to make sure they pass
Assistant
Let me also run a broader set of tests to make sure nothing is broken
Assistant
Let me also run some broader tests related to variance and type aliases
Assistant
Let me run the broader ty tests
Assistant
Let me check test results for all three crates more carefully
Assistant
Let me run all the ty-related tests to make sure nothing is broken
Assistant
Let me check the hover test for type alias variance too
Assistant
Let me search for any mdtest that tests nested type alias variance
Assistant
Let me check the variance mdtest to see what tests exist for type alias variance
Assistant
Let me verify the changes by looking at the diff
Assistant
All tests pass. Let me also run a broader set of tests to ensure nothing is
Assistant
All tests pass. Let me verify the final state of my changes one more time.
Assistant
Summary of Changes
The issue was that type alias variance inference stopped tracking after two levels of nesting (e.g.,
type RefRefBox[T] = RefBox[T]wheretype RefBox[T] = Box[T]). The root cause was in theKnownInstanceType::variance_ofimplementation, which simply calledtype_alias.raw_value_type(db).variance_of(db, typevar), ignoring any specialization on the type alias.Changes Made:
-
Fixed
crates/ty_python_semantic/src/types/known_instance.rs:- Updated
VarianceInferableimplementation forKnownInstanceTypeto properly handle type alias specialization
- Updated
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.