Type alias statements stop tracking typevar variance after two deep Summary Ran into…
- User
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.
- Assistant
Let me reproduce the issue first.
- Assistant
The assignability path already works here (may have been partially fixed since 0.0.35). Let me check how variance itself is inspected in mdtests.
- Assistant
Let me check how legacy generic aliases behave and are represented.
- Assistant
There's an existing unit test
type_alias_variancein tests.rs — that's exactly where I can demonstrate the bug and the fix. Let me first confirm the current bug at that level: - Assistant
Now the dispatching impl for
TypeAliasType: - Assistant
Now update
Type::variance_ofin types.rs and theKnownInstanceTypepath: - Assistant
Test passes. Let me verify the new assertions actually fail without the fix (to confirm they cover the bug):
- Assistant
With the fix,
Top[RefRef[Any]]correctly staysTop[Invariant[Any]]. Verify the pre-fix behavior differs (so mdtest coverage is meaningful): - Assistant
Materialization resolves aliases first, so no mdtest delta there. Check the constraint-solving consumer at generics.rs:2820:
- Assistant
The hover output is the user-visible surface (what the playground showed). Let me add a nested-alias hover test:
- Assistant
Now add mdtest coverage. Extend the "Type aliases" section in the pep695 variance mdtest:
- Assistant
Legacy aliases resolve eagerly, but let me add mdtest coverage there too documenting the contract:
- Assistant
Legacy
TypeVarwithout explicit variance is invariant by declaration. Fix the legacy mdtest to use properly-declared typevars: - Assistant
Both pass. Now run the full test suites to catch regressions:
- Assistant
Now clippy and the remaining checks:
- Assistant
No
uv/prekin this environment — run the equivalent checks manually: - Assistant
All green. Final review of the complete diff:
- Assistant
Everything is in place. Summary of the fix:
Problem
Variance inference stopped at any reference: in , fell into the bivariant catch-all (). So while inferred correctly (its RHS is a nominal instance), inferred as bivariant — the RHS is an unresolved alias reference which contributed nothing. Bivariance makes every specialization mutually assignable, enabling the reported "type-safe transmute".
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.