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's look at the exact match block to edit it correctly.
Assistant
Good, it compiles. Now let's write a more targeted test to check where
variance_of(not full relation-based subtyping) is actually consumed, so we can verify the fix actually affects behavior somewhere observable.Assistant
Now update the
Type::TypeAliasarm in types.rs similarly:Assistant
Now let's add a dispatch method on
TypeAliasTypeenum and forManualPEP695TypeAliasType(which has no specialization concept, so justvalue_type).Assistant
Now update
known_instance.rsandtypes.rsto usevariance_value_typeinstead ofvalue_type/raw_value_type.Assistant
All tests pass including the new multi-level, reordered, and multi-param cases. Now let's revert the temporary test additions (since we shouldn't add new tests to the repo) and re-run full test suite for regressions.
Assistant
Good, the test file is reverted to original. Now let's run the existing full test suite for
ty_python_semanticto check nothing broke, plus verify the original issue example manually.Assistant
pep695_type_aliases.md causes stack overflow. Let's inspect that file for likely recursive alias scenarios.
Assistant
Confirmed: this test passes on original code, and my changes cause infinite recursion for self-referential generic aliases like
G[T] = G[T]. Let's restore the fix and figure out how to guard cycles properly.Assistant
Good, both are
Copy + Eq + Hash. Now let's implement a thread-local recursion guard for type alias variance computation.Assistant
Good,
pub(crate). Let's fix the import path — should usecrate::types::variance::TypeVarVarianceandVarianceInferable, andcrate::types::BoundTypeVarInstance(check it's exported at that path).Assistant
Good,
crate::types::BoundTypeVarInstanceworks. Now let's add the recursion-guardedvariance_ofmethod toTypeAliasType.Assistant
Excellent — this is exactly the utility I need, with panic-safety too. Let's rewrite my addition using
ActiveRecursionDetectorkeyed by(salsa::Id, salsa::Id)instead of the unsafe transmute.Assistant
use salsa::plumbing::AsId;— good, that's the import path. Now let's rewrite the type_alias.rs change usingActiveRecursionDetectorwith a proper key struct instead of the unsafe transmute.Assistant
Now let's rewrite the
variance_ofmethod to useActiveRecursionDetectorwith a lifetime-free key.Assistant
Now update the callers (
types.rsandknown_instance.rs) to callTypeAliasType::variance_ofinstead of directly calling.variance_value_type(db).variance_of(...).Assistant
It compiles. Now let's re-run the mdtest suite, including the pep695_type_aliases and legacy variance tests, plus my earlier temporary variance test.
Assistant
All 338 mdtests pass. Now let's re-run the internal unit tests including our temporary variance test scenario to confirm the fix truly works with the deeper recursion guard in place.
Assistant
All pass including three-level, mutually recursive, and self-specializing edge cases. Now let's revert the temporary test additions and run the full mdtest suite once more plus the full workspace test suite to check for regressions.
Assistant
Good, tests.rs is back to original. Now let's run the full test suite (all crates that might depend on this) to double-check no regressions, plus re-verify the original issue repro scripts.
Assistant
Now let's confirm this was NOT correctly detected before the fix (bug reproduced), by stashing and re-testing.
Assistant
Good, fix is restored. My earlier "buggy code" test result was actually already fixed (my stash pop after the first test wasn't done correctly, so both tests ran against the fixed code!). Let's redo the comparison test properly.
Assistant
Now let's stash properly to test the "before" state and verify it does NOT error (bivariant bug).
Assistant
Confirmed:
Invarianton fixed code. Now let's check on the original code (stash) to see what it produced.Assistant
Let's fix the test snippet to correctly use it.
Assistant
Now we have trace logs. Let's find the trace for Container's own variance and the box attribute variance.
Assistant
Let's revert my temporary tests.rs debug additions to the clean baseline, and try a class with explicit attribute annotation using the alias type directly.
Assistant
Now let's verify this fails to detect the error on the original (buggy) code.
Assistant
Confirmed end-to-end: this is a genuine, user-visible type-safety bug (bivariant
Containerin the original code allows unsound covariant/contravariant substitution), and it's fixed by our change. Let's restore the fix.Assistant
We now have solid end-to-end confirmation. Let's rebuild and run the full mdtest + unit test suite one final time to make sure everything is clean.
Assistant
Now let's run the full test suite for
ty_python_semanticto make sure nothing else broke.Assistant
All tests pass. Let's confirm the unit tests portion (not just mdtest) also passed fully by checking the summary further up.
Assistant
All 203 lib tests pass (including
type_alias_variance), plus all 338 mdtest tests. Let's also run the broader workspace test suite for any other crates that might reference variance/type_alias APIs (e.g.,ty_ide,ty_project).Assistant
Entire workspace compiles cleanly. Let's run clippy to check for lint issues in the changed files.
Assistant
Just one style warning. Let's fix that lifetime elision.
Assistant
Clean. Let's do a final full check: rebuild, run the full test suite once more, and review the final diff.
Assistant
All tests pass. Let's review the final diff.
Assistant
No warnings. Let's clean up temp files and do a final sanity check with
git status.Assistant
Only the intended source files are modified;
tests.rsis back to its pristine state. Let's do one more full test run as a final gate before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.