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 to understand its structure and locate relevant files. Let's first get an overview.
- Assistant
Let me explore the repository structure more to understand the codebase, particularly focusing on type variance and type alias handling.
- Assistant
Let me look at the issue more closely. The issue is about type alias variance tracking. Let me search for variance-related code in the repository.
- Assistant
Let me look at the variance module specifically and understand how type aliases handle variance.
- Assistant
Now let me look for how type aliases handle variance inference. The issue is about type alias statements stopping tracking typevar variance after two levels deep. Let me search for type alias handling code.
- Assistant
Let me search for how variance is inferred for type aliases, particularly looking at
TypeAliastypes. - Assistant
Let me look at the main types.rs file for the VarianceInferable implementation for TypeAlias.
- Assistant
Let me look at the VarianceInferable implementation for Type.
- Assistant
Found the issue! In the
VarianceInferableimplementation forType, theType::TypeAlias(_)variant returnsTypeVarVariance::Bivariantunconditionally. This means when we havetype RefRefBox[T] = RefBox[T], the inference seesRefBoxas aTypeAliasand immediately returnsBivariantinstead of looking through the alias to see the actual type it resolves to.Let me look at the TypeAlias structure:
- Assistant
Let me search for TypeAlias type definition.
- Assistant
I see.
Type::TypeAliaswraps aTypeAliasType<'db>. Let me look at theTypeAliasTypestruct to understand its API, particularlyvalue_type. - Assistant
Let me look at the TypeAliasType to understand what value_type returns and how to access it.
- Assistant
Now I understand the structure. The issue is clear: in the
Type::TypeAlias(_)variant, it returnsTypeVarVariance::Bivariantwithout looking through the alias to its value type. Let me look at how variance is inferred forGenericAlias: - Assistant
Let me look at the GenericAlias variance_of implementation.
- Assistant
Let me search for the variance_of implementation for GenericAlias
- Assistant
Let me see the implementation:
- Assistant
- Assistant
Let me look at the KnownInstance variance implementation.
- Assistant
Now I can see the issue clearly. Looking at lines 6870-6871 in types.rs:
| Type::TypeAlias(_) // <-- Returns Bivariant unconditionallyBut the
KnownInstanceType::TypeAliasTypevariant in known_instance.rs correctly handles variance:KnownInstanceType::TypeAliasType(type_alias) => { type_alias.raw_value_type(db).variance_of(db, typevar) }So the fix is to have
Type::TypeAliasalso recurse through to the value type. Let me check whatType::TypeAliasrepresents: - Assistant
Let me look at the line where
Type::TypeAlias(_)falls in the match, around line 6870: - Assistant
Now I see the issue. The
Type::TypeAlias(_) => TypeVarVariance::Bivariantline at line 6870 is the problem. When computing variance through a chain likeRefRefBox[T] = RefBox[T], theGenericAlias::variance_ofneeds to compute variance of the originType::TypeAlias(RefBox), but this returnsBivariantinstead of looking through the alias to the value type.Let me first create a test to reproduce the issue:
- Assistant
Let me look at existing variance tests:
- Assistant
Let me search more broadly for variance tests.
- Assistant
Let me look at the tests file for variance-related tests.
- Assistant
Let me look at the existing
type_alias_variancetest: - Assistant
Now I understand the issue completely. The
KnownInstanceType::TypeAliasTypepath already correctly delegates toraw_value_type(db).variance_of(db, typevar), but theType::TypeAlias(_)path returnsBivariantunconditionally. When a type alias appears as theoriginof aGenericAlias(e.g.,RefBox[T]), it's represented asType::TypeAlias(RefBox), and the variance computation fails.Let me implement the fix:
- Assistant
Now let me implement the fix. I need to move
Type::TypeAlias(_)out of the catch-all arm and add a proper implementation: - Assistant
Good, the fix is in place. Now let me verify the change compiles and the existing tests pass:
- Assistant
Good, the existing test passes. Now let me verify the fix works by running a quick check. Let me also look for any existing mdtest files for type alias variance:
- Assistant
Let me look for mdtest files about type alias variance specifically.
- Assistant
Let me search for files related to type alias variance more broadly:
- Assistant
Let me check the variance mdtest files.
- Assistant
I see there are existing mdtest files for variance. The section "Type aliases" already tests direct aliasing but not double-aliasing (alias of an alias). Let me also check the legacy variance test:
- Assistant
Now let me run the existing tests to see if our fix works. Let me first run the test to verify the issue is fixed:
- Assistant
Good, the existing test still passes. Now let me add test cases for the double-alias chain to the existing test. Let me look at the test more carefully:
- Assistant
Now let me add test cases for the double-alias chain. I need to add double-alias declarations and test the variance of the outer alias's typevar. Let me add the test cases:
- Assistant
Now I'll add test cases for the double-alias chain. Let me add double-alias declarations to the test source code and then add assertions:
- Assistant
Now let me add the assertions for the double and triple aliases:
- Assistant
Let me check the file around line 456 to make sure there's no syntax error:
- Assistant
I see there's an extra
}at line 457. Let me fix that: - Assistant
Good, the file looks correct now. Let me run the test to see if it passes:
- Assistant
All tests pass including the new double-alias and triple-alias test cases. Let me also run the existing tests more broadly to make sure I haven't broken anything.
- Assistant
All unit tests pass. Now let me run the variance-related mdtest files to make sure they still pass:
- Assistant
Let me find the correct test name for the variance mdtest:
- Assistant
All variance-related tests pass. Let me also run a broader set of tests to make sure I haven't broken anything else. Let me run all the mdtest files related to generics:
- Assistant
All generics-related tests pass. Let me also run the type_properties tests since they might be affected by variance changes:
- Assistant
All tests pass. Let me also check if there are any other relevant tests I should run. Let me check for tests related to type aliases.
- Assistant
Oh no, there's a stack overflow! This is likely caused by infinite recursion through recursive type aliases. Let me revert my change and implement a better approach that uses salsa cycle detection:
- Assistant
Now I need to add the
type_alias_variance_oftracked function with cycle detection. Let me add it: - Assistant
Good, the function is added. Now let me check if the code compiles and the tests pass:
- Assistant
The
aliasis a reference. Let me fix the dereference: - Assistant
Let me fix the dereference - since
TypeisCopy,aliasshould already be a value: - Assistant
Now let me try to compile and run the tests:
- Assistant
The test passes. Let me now run the broader set of tests including the pep695 tests that caused the stack overflow:
- Assistant
No more stack overflow. Let me run the full set of generics and type_properties tests:
- Assistant
All generics tests pass. Let me also run the type_properties tests:
- Assistant
All tests pass. Now let me consider the evaluation contract: "Preserve covariant, contravariant, invariant, and bivariant results through multi-level and reordered multi-parameter alias chains." Let me add a test for reordered multi-parameter alias chains.
Let me add multi-parameter test cases to the existing test:
- Assistant
Let me add multi-parameter test cases. I need to add a helper function to get a specific typevar by index, and then add test cases for reordered multi-parameter aliases:
- Assistant
Now I need to add a helper function to get a specific typevar by index, and then add assertions for the multi-parameter alias test cases. Let me add the helper function and assertions:
- Assistant
I see there's a syntax error - the
elseblock is missing. Let me look at the code and fix it: - Assistant
I see the issue - the
get_type_aliasfunction's closing brace was removed. Let me fix the structure: - Assistant
Wait, I notice the
let mut db = setup_db();line is missing. Let me check and fix: - Assistant
I need to add
let mut db = setup_db();back. Let me fix: - Assistant
Let me fix the missing
let mut db = setup_db();line: - Assistant
Now let me add the assertions for the multi-parameter alias test cases. Let me find where to add them:
- Assistant
Let me find the end of the test function and add the multi-parameter assertions there:
- Assistant
Let me find the exact end of the test function:
- Assistant
Now let me add the multi-parameter assertions before the closing
}: - Assistant
Good, the test is added. Now let me run the test to see if it passes:
- Assistant
The multi-parameter test is failing because I had the wrong expectations. Let me think about the correct variances:
For
CoContra[A, B]:Ais covariant (used in return position)Bis contravariant (used in argument position)
For
Reordered[X, Y] = CoContra[Y, X]:Xmaps toBposition → contravariantYmaps toAposition → covariant
For
DoubleReordered[P, Q] = Reordered[Q, P]:Pmaps toYposition (covariant) → P is COVARIANTQmaps toXposition (contravariant) → Q is CONTRAVARIANT
I had the expectations for
DoubleReorderedswapped. Let me fix the test: - Assistant
Good, now let me run the test again to see if it passes:
- Assistant
All tests pass. Now let me run a broader set of tests to make sure nothing is broken. Let me run the full test suite for the ty_python_semantic package:
- Assistant
All tests pass. Let me also run the full mdtest suite to make sure nothing is broken:
- Assistant
All 338 mdtest tests pass. Let me also run the broader test suite to make sure nothing else is broken:
- Assistant
All tests pass. Let me now review my changes to make sure they are complete and correct. Let me look at the final state of the changes:
- Assistant
The changes look correct and complete. Let me now verify the fix resolves the exact issue from the bug report by running a quick check:
- Assistant
Let me try with the correct pattern:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.