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
Build succeeded. Let's check current behavior before my fix actually took effect (it did build with fix). Let's test with the ty binary.
Assistant
Now let's confirm the fix actually addresses the bug by temporarily reverting the fix and re-testing.
Assistant
Reproduced the bug with unfixed code:
Outer[T]with a field of typeRefRefBox[T]incorrectly becomes bivariant. Now let's rebuild with fix and confirm resolution.Assistant
Fixed. Now let's test the "one-level" case still works (regression check) and also test covariant/contravariant/multi-param reordering scenarios, plus recursive alias cycles.
Assistant
Now let's test more comprehensive scenarios: contravariant, multi-param reordering, and recursive cycles using the Outer-wrapper approach that exercises the real bug path.
Assistant
All variance kinds pass with the fix at 2-level alias nesting. Now let's test multi-parameter reordering and recursive/mutual cycles.
Assistant
All pass. Now let's test recursive/mutually recursive alias cycles for safe termination (bivariant fallback).
Assistant
Good, interned structs have
.as_id()via theAsIdtrait from salsa. Now let's implement the fix with a recursion guard.Assistant
Now let's implement the recursion guard. I'll add a thread-local detector keyed by
(salsa::Id of TypeAliasType, salsa::Id of BoundTypeVarInstance).Assistant
salsa::Idis already used without explicit import (must be accessible via thesalsacrate directly). Let's checkAsIdtrait import in function.rs.Assistant
Now let's add the AsId import and the recursion-guard code before the
impl<'db> VarianceInferable<'db> for Type<'db>block.Assistant
Now update the
Type::TypeAliasarm to use this helper function.Assistant
Now let's also update
known_instance.rs'sTypeAliasTypevariance_of to useraw_value_typewith the same style of guard (usingtype_alias_variance_ofisn't directly applicable since it usesvalue_typenotraw_value_type). Let's check that code again.Assistant
Build succeeded. Now let's re-run all our tests.
Assistant
No stack overflow (test15 works fine, expected outputs test2/3/4/5/10/13 are known-wrong-expectations of mine, not real bugs). test6/9/12/14 (the actual valid tests) all pass. Now let's run the existing repo test suite and mdtest for variance to make sure nothing broke.
Assistant
Now let's run the broader mdtest suite for generics and type_alias to check nothing else broke.
Assistant
All 338 tests pass. Let's check the legacy generic type alias mdtest for coverage and also check legacy generic aliases (typing.TypeAlias / TypeVar-based) to make sure the fix covers them too, per the interface contract mentioning "legacy generic type aliases."
Assistant
4-level chain works. Now let's double check the diff and run the full test suite once more, plus clean up temp files.
Assistant
This looks clean and minimal. Let's run the full test suite once more (including clippy check for warnings) to be thorough.
Assistant
No warnings. Let's run the full workspace tests for ty_python_semantic once more to be safe, and check clippy.
Assistant
No new clippy warnings for our file (the pre-existing warning is unrelated to our change). Let's now run the complete test suite once more for good measure and clean up temp files.
Assistant
All tests pass. Let's clean up temporary test files.
Assistant
Summary
Root cause: In
crates/ty_python_semantic/src/types.rs, the implementation for treated (a specialized reference to a type alias appearing as part of another type, e.g. used inside ) as unconditionally , instead of recursing into the alias's value type. This meant that as soon as a type alias's RHS referenced another (specialized) type alias, variance information was lost from that point onward — exactly the "two deep" bug described in the issue.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.