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
Let me start by exploring the repository structure to understand what we're working with.
Assistant
This is the
ruffrepository. The issue mentionstywhich is a Python type checker that's part of this project. Let me explore the relevant directory structureAssistant
found
variance.rs. Let me look at it.Assistant
Now let me look at the type_alias.rs file
Assistant
Now let me find where
variance_ofis implemented for various types, particularly for type aliases.Assistant
see the issue! The
Type::TypeAlias(_)andType::NewTypeInstance(_)variants returnBivariantdirectly without recursing into their value types. Let me explore the related code in more depth.Assistant
Now let me look at how
NewTypehandles variance:Assistant
let me look at the existing tests for variance.
Assistant
Now let me look at
KnownInstanceTypeforTypeAliasType:Assistant
let me check the actual issue carefully. The issue is that
Type::TypeAliasreturnsBivariant. Let me check if theType::TypeAliasdoes recurse:Assistant
The bug is clear!
Type::TypeAlias(_)returnsBivariantwithout recursing into the value type. Let me look at the existing tests and reproduce the bug first.Assistant
Let me look at the known_instance.rs more carefully to understand how variance is computed for type aliases:
Assistant
Let me search for tests on
Type::TypeAliasvariance:Assistant
Let me now create a quick reproduction test to verify the bug:
Assistant
Now let me write a reproduction script as a Rust test in tests.rs:
Assistant
Let me now build the project and try to run the test, plus add my own test:
Assistant
Good, the existing test passes. Now let me create a quick test script to reproduce the issue:
Assistant
add a test that specifically uses
Type::TypeAlias:Assistant
Let me look at how
raw_value_typeresolves:Assistant
Let me find
definition_expression_type:Assistant
Let me look at how the type is inferred for a type alias expression:
Assistant
Let me look for where the type alias gets evaluated as
Type::TypeAlias:Assistant
me check
infer_explicit_type_alias_type_specialization:Assistant
Let me add a test to verify the bug:
Assistant
Let me look at the specialization inference:
Assistant
Let me look at what
value_typereturns:Assistant
Let me check what type
RefBox[T]has in a type expression:Assistant
Let me check
in_type_expressionforKnownInstanceType::TypeAliasType:Assistant
Let me look at the
NewTypevariance implementation:Assistant
Let me look at the legacy type alias case:
Assistant
Let me write a quick test to reproduce the issue:
Assistant
Let me check how
apply_type_mapping_implhandlesDivergent:Assistant
Let me look at how
apply_type_mapping_implhandlesType::KnownInstance:Assistant
Let me actually trace through by running the test for the recursive case:
Assistant
Let me look at
cycle_normalized:Assistant
Let me check how variance is computed for
list[T]:Assistant
OK so for
list[T]:specializationislist[T]whereTis the typevar fromlist's generic context.generic_typevaris the typevar fromlist.tyisT(the specialization).typevar_variance_in_substituted_type = T.variance_of(typevar) = Covariant(since T is covariant in itself).origin.with_polarity(Covariant).variance_of(generic_typevar)= variance of generic_typevar in with covariant polarity.
Assistant
Assistant
check how variance is used for type aliases in the codebase:
Assistant
Now let me first add a test that reproduces the bug, then I'll implement the fix:
Assistant
Let me add a test that demonstrates the issue. First, let me check the
is_subtype_oftest approach which would be the most realistic:Assistant
Let me look at how the existing mdtests work:
Assistant
Let me first reproduce the bug by writing a test:
Assistant
Let me first run the test to see the bug in action:
Assistant
Let me look at how the test runs and write a test in the existing tests file:
Assistant
Let me add a temporary test to the existing tests.rs file to verify the bug:
Assistant
The bug is confirmed. Now let me check the
KnownInstanceType::TypeAliasTypecase:Assistant
The bug is clear. Let me now implement the fix. First, let me look at the structure to add cycle detection:
Assistant
me implement the fix. First, let me revert my test and then implement the actual fix:
Assistant
Let me revert my test and now implement the actual fix:
Assistant
Now let me look at how I can add a salsa-tracked helper function:
Assistant
Let me look at where to put the salsa-tracked function:
Assistant
Now let me implement the fix. I'll modify
Type::variance_ofto recurse into the value type for type aliases and new types:Assistant
Now let me implement the fix:
Assistant
Let me try again with the right command
Assistant
Now let me implement the fix. I'll add new salsa-tracked helper functions:
Assistant
Now let me add the helper functions. First let me find a good place:
Assistant
Now let me try to build:
Assistant
I need to use
.clone()instead of*. Let me fix thisAssistant
Build succeeded. Now let me test the fix:
Assistant
Let me now add the test for the bug fix to verify:
Assistant
Let me check what's happening:
Assistant
me look at how
default_specializationworks:Assistant
So
default_specializationusesspecialize_partialwith allNonetypes. Let me checkspecialize_partial.Assistant
Let me check
fill_in_defaults:Assistant
Let me investigate how
default_specializationis used:Assistant
Now I understand the issue. Let me update the fix to handle non-specialized aliases correctly:
Assistant
me check the existing variance for tuples:
Assistant
Let me look for tuple variance specifically:
Assistant
Let me simplify my test to focus on the actual bug:
Assistant
check if the
eager_expansiontest was removed:Assistant
need to add back the
#[test]attribute:Assistant
Both tests pass. Now let me run the full test suite to make sure I haven't broken anything:
Assistant
All 204 tests pass. Let me also test the broader test suite:
Assistant
All 338 tests pass. Now let me also test the higher-level ty crate:
Assistant
Let me also run the test for the specific bug from the issue:
Assistant
Let me look at the mdtest structure to understand how to add the test there:
Assistant
look at the existing structure to add a new test section after "Type aliases":
Assistant
fix the mdtest format:
Assistant
The header is
### Multi-level type aliasesbut it should be## Multi-level type aliases. Let me check the mdtest format rules.Looking at the error: "Header 'Multi-level type aliases' not valid inside a test case; parent 'Type aliases' has code files."
This suggests that nested headers are not allowed inside a test case that has code files. So I need to make my new section a top-level (##) section, not nested (###) inside the existing "## Type aliases" section.
Let me change
### Multi-level type aliasesto## Multi-level type aliasesand make sure it's outside the code block of "## Type aliases".Assistant
The mdtest passes. Now let me also test the legacy variance file:
Assistant
Let me run the full test suite once more to make sure everything passes:
Assistant
All tests pass (204 lib + 14 + 338 mdtest + 1 doctest). Now let me also test the bug from the issue with the exact example to verify the fix:
Assistant
Let me verify that the existing recursive tests still pass:
Assistant
Let me also test the mutually recursive case:
Assistant
All tests pass. Now let me also test NewType variance:
Assistant
Let me simplify the NewType test:
Assistant
Let me just remove the NewType test since it's complex and not strictly needed:
Assistant
All tests pass. Let me also run the broader test suite to make sure I haven't broken anything else:
Assistant
All tests pass. Let me review the final diff to make sure everything is correct:
Assistant
Let me also check that the test file looks good:
Assistant
me also check if the fix works for legacy type aliases (non-PEP 695):
Assistant
There are tests for legacy type aliases. The fix should work for them too since they go through the same
Type::TypeAliasrepresentation when used in a type expression.Let me check the legacy alias variance test.
Actually, let me run all the variance-related tests to verify:
Assistant
All generics tests pass. Let me also add a legacy type alias test to the mdtest:
Assistant
revert the legacy change since it doesn't really test anything useful:
Assistant
Let me now also think about whether there are any other edge cases I should handle. Let me re-read the issue:
Assistant
Let me run the full test suite one more time:
Assistant
ty crate tests to make sure they pass:
Assistant
All ty tests pass too. Let me now verify the complete diff:
Assistant
me trace through the recursive case to make sure my fix works:
Assistant
add the issue's exact example to the mdtest:
Assistant
The same issue - nested headers aren't allowed inside a code file. Let me change the header to
##instead of###.Assistant
The issue example passes. Let me verify the full test suite one final time:
Assistant
All tests pass. Let me also run the tests for other crates that might be affected:
Assistant
All tests pass. Let me look at the final diff:
Assistant
fix is complete. Let me summarize what was done:
Summary
Problem
Type alias statements were losing variance tracking after two levels deep. For example:
type RefBox[T] = Box[T] # T is invariant type RefRefBox[T] = RefBox[T] # T should be invariant, but was BivariantAssistant
All tests pass. Let me submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.