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's view the full test file's relevant test function
type_alias_variance.Assistant
Now let's look at the full end of the file to know where to add impls, and check
GenericContextimport etc.Assistant
Now let's check the
variancemodule path andBoundTypeVarInstancelocation for import.Assistant
Now let's add the
VarianceInferableimpl forPEP695TypeAliasTyperight after its main impl block, and forTypeAliasTypeandManualPEP695TypeAliasType.Assistant
Insert new tracked impl block for
PEP695TypeAliasType::variance_ofafter line 147 (end of its main impl block).Assistant
Now add
VarianceInferableimpl forTypeAliasTyperight after its impl block ends (find matching closing brace).Assistant
Now update
Type::TypeAliasbranch in types.rs andKnownInstanceType::TypeAliasTypebranch in known_instance.rs to useTypeAliasType::variance_of.Assistant
Good. Now let's try building the crate.
Assistant
Compiles. Now let's write a quick reproduction test to confirm the fix works, using a temporary addition to
tests.rsthen reverting.Assistant
The fix works. Now let's revert the temporary test and run the existing test suite to make sure nothing is broken.
Assistant
Now let's run the existing
type_alias_variancetest and other related tests.Assistant
All existing tests pass. Let's also run the whole
ty_python_semantictest suite to check for regressions in variance/related mdtests.Assistant
All pass. Now let's search for mdtest files related to variance / generics / type aliases to run those too.
Assistant
Let's search for type alias variance content in these mdtest files, and see if there's a dedicated section for type alias variance and reordered/mixed-multi-param tests.
Assistant
Multi-param reordering works correctly too. Now let's test the legacy generic type alias case using an mdtest-style approach with
static_assert/is_subtype_ofthrough actual type-checking (via a quick mdtest run), to confirm legacy alias chains also track variance correctly (likely already worked, but let's verify explicitly).Assistant
Good — legacy generic type alias chains already track invariance correctly (no diagnostics, meaning the static_asserts passed, confirming it's not bivariant). Now let's clean up the temporary test additions.
Assistant
Excellent, cycles terminate correctly with the Bivariant fallback. Now clean up the temp test again.
Assistant
Now let's run the full mdtest suite for the affected areas (generics/variance, type aliases) to ensure nothing regressed.
Assistant
Both pass. Now let's run the full mdtest suite to check for any regressions across the whole test corpus (may take a while).
Assistant
All 338 mdtest suites pass. Now let's run the entire
ty_python_semantictest crate (lib + integration) fully to be thorough, plus check the broader workspace builds.Assistant
Everything passes. Let's do a final review of the diff.
Assistant
The diff is clean, minimal, and targeted. Let's run
cargo fmt --checkon the modified files to ensure formatting matches conventions.Assistant
Now let's do a final full re-check to be safe after formatting.
Assistant
Let's view final diff once more and confirm status of the repo before submitting.
Assistant
Let me explicitly verify the bug reproduction against the original (unfixed) code, to satisfy the checklist requirement.
Assistant
Confirmed: pre-fix result is
Bivariant(the bug). Now restore the fix.Assistant
Fix restored. Let's do one final full test pass to confirm everything is good before submission.
Assistant
All tests pass. Let's finalize by reviewing the checklist items.
Checklist review
-
If an error was reported in the issue description, I have successfully reproduced it. ✅ I traced the bug to the
Type::TypeAlias(_) => TypeVarVariance::Bivariantcatch-all arm intypes.rs'sVarianceInferableimpl, which is exactly the type produced when a specialized PEP 695 alias (e.g.RefBox[T]) appears as the RHS of another alias. I explicitly reverted my fix (viagit stash), added a temporary test reproducingtype RefBox[T] = Invariant[T]; type RefRefBox[T] = RefBox[T], and confirmed it printedBivariantinstead of the expectedInvariant, matching the issue. -
✅ After restoring the fix, the same scenario (and the covariant 3-level chain, an invariant 2-level chain, reordered multi-parameter chains, and legacy generic alias chains) all produced correct, non-bivariant results, verified via temporary tests that were removed afterward (not left in the repo).
-
Assistant
This looks complete and correct. Submitting now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.