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.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /ruff, current file: none) bash-$
Assistant
Now let's look at the class variance code and the tracked query with cycle handling.
Assistant
Now add the
VarianceInferableimpls after thePEP695TypeAliasTypeimpl block.Assistant
Now add the
ManualPEP695TypeAliasTypevariance query and theTypeAliasTypedispatch.Assistant
Now add the
TypeAliasTypedispatch impl at the end of its impl block.Assistant
Now update
known_instance.rsandtypes.rs.Assistant
Now update
types.rsfor theType::TypeAliasarm.Assistant
Now run the mdtest suite (the big one) and the ty_ide tests.
Assistant
I've completed the fix. Here's my summary and the checklist review:
Change summary
Root cause:
Type::TypeAlias(_)(a type alias appearing inside a type expression, e.g.RefBox[T]intype RefRefBox[T] = RefBox[T]) was unconditionally treated asBivariantby variance inference, so any typevar that only reached a class through a nested alias lost its variance.Fix (3 files):
types/type_alias.rs:- New tracked query
PEP695TypeAliasType::unspecialized_variance_of(cycle_initial →Bivariant) which recurses intoraw_value_type. It's always invoked on the unspecialized alias, so the set of possible cycle participants is finite and Salsa's bivariant cycle fallback terminates (mutually) recursive aliases (including "growing" ones liketype Growing[T] = Growing[list[T]] | None). VarianceInferable for PEP695TypeAliasType: for a specialized alias it mirrorsGenericAlias::variance_of— composing the variance of the queried typevar in each substituted type with the variance of the corresponding alias typevar in the alias body (this handles reordered/multi-parameter alias chains).ManualPEP695TypeAliasType(legacyTypeAliasType(...)constructor aliases) gets an analogous tracked, cycle-safevariance_of, plus aTypeAliasTypeenum dispatch impl.
- New tracked query
types/known_instance.rs:KnownInstanceType::TypeAliasTypenow delegates to the new (specialization-aware, cycle-safe) alias implementation.types.rs:Type::TypeAlias(alias)now recurses instead of returningBivariant.
Checklist
- Reproduced the reported error: Yes. Subtyping asserts don't observe alias variance (aliases are expanded), so I reproduced at the semantic level with a temporary unit test using the same entry point as the existing
type_alias_variancetest:Cov2/Inv2/Contra2(two-deep aliases) returnedBivariantbefore the fix (confirmed by stashing my source changes:left: Bivariant, right: Covariant). - Confirmed the fix resolves it: Yes. With the fix, one/two/three-level chains yield Covariant/Contravariant/Invariant/Bivariant correctly, contravariant-of-contravariant flips to Covariant, and a reordered two-parameter chain (
type Two2[T, U] = Two1[U, T]) gives the swapped variances. - Edge cases: recursive (
type Rec[T] = None | list[Rec[T]]→ Bivariant), partially recursive (Rec2→ Invariant), mutually recursive (G/H→ Bivariant), unboundedly "growing" alias specializations (Growing[list[T]]→ Bivariant, terminates), bare/unspecialized nested aliases, explicit-variance typevars, and manualTypeAliasType(...)aliases (including recursive ones) were all exercised in temporary tests and terminate with sensible results. - Existing tests pass:
cargo test -p ty_python_semantic --lib(203 passed, incl.type_alias_variance),--test mdtest(338 passed),--test corpus,cargo test -p ty_ide(1487 passed),cargo test -p ty, pluscargo clippy --all-targets,cargo check --workspace --all-targetsandcargo fmt --check. All temporary test code was removed (git statusshows only the three source files modified).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →