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
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.
- Assistant
Now let me look at how the mdtests for variance are organized, and check the
GenericContext/SpecializationAPI used byGenericAlias: - Assistant
Let me reproduce the bug first:
- Assistant
The bug reproduces at HEAD. Now let me implement the fix. First, check imports in
type_alias.rsand theSpecializationAPI: - Assistant
Now implement. Edit
type_alias.rs: - Assistant
Now add the
VarianceInferableimpls. Add after thePEP695TypeAliasTypetracked impl block: - Assistant
Now the
ManualPEP695TypeAliasTypeimpl and the dispatchingTypeAliasTypeimpl: - Assistant
Now update
Type::variance_ofin types.rs: - Assistant
Now update
known_instance.rsto route through the new tracked query: - Assistant
Now let me extend the Rust unit test with multi-level chains, reordered params, and mutual recursion:
- Assistant
Now extend the pep695 variance mdtest's "Type aliases" section:
- Assistant
Now rebuild and test the legacy (manual
TypeAliasType) chain before adding its mdtest: - Assistant
Let me check how PEP-613/implicit legacy aliases behave — these may not involve
Type::TypeAliasat all: - Assistant
Wait — even the no-alias
Directcase fails, yet my earlier PEP 695 test passed. Let me compare directly: - Assistant
Now add manual ("legacy")
TypeAliasTypecoverage to the Rust unit test. Check how to extract the typevar from a nominal instance: - Assistant
String forward refs work and mutual recursion resolves. Now add the manual-alias unit test:
- Assistant
Legacy plain
TypeVars have declared invariant variance — my test classes need explicitly-variant typevars. Fix the test source: - Assistant
Both unit tests pass. Now run the variance mdtests:
- Assistant
Now run the full
ty_python_semantictest suite: - Assistant
Clippy is clean. Final verification of the original issue and review of the complete diff:
- Assistant
prek/uvaren't available in this environment. Let me run the equivalent checks manually — rustfmt on the Rust files and ruff-format on the mdtest markdown: - Assistant
Now check mdtest formatting with the local ruff build:
- Assistant
All green. Final verification against the original issue's exact reproduction:
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.