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 set up a reproduction to see current behavior:
- Assistant
Now let me confirm the bug with
is_subtype_ofprobes: - Assistant
The probe passed — subtype checks likely expand the alias. Let me find where alias typevar variance is actually consulted:
- Assistant
Now implement the fix. First in
type_alias.rs: - Assistant
Good, import paths match. Now add the
VarianceInferableimpls intype_alias.rs: - Assistant
Now update
Type::variance_ofin types.rs andKnownInstanceType::variance_of: - Assistant
Now update
KnownInstanceType::variance_ofto route through the new tracked impl: - Assistant
Legacy works via eager expansion. Now add mdtests. Insert new sections after the existing "Type aliases" section in
pep695/variance.md: - Assistant
Now add a legacy section to
generics/legacy/variance.md: - Assistant
Now run the variance mdtests:
- Assistant
Legacy typevars default to explicit invariance — my covariant example needs
covariant=True: - Assistant
Both pass. Now run the full
ty_python_semantictest suite to check for regressions: - Assistant
Clippy is clean. Run tests for dependent ty crates:
- Assistant
While that runs, let me review the full diff:
- Assistant
The diff looks good. Let me check the background test run and then run
uvx prek: - Assistant
The background run is still compiling. While waiting, let me verify the original issue repro once more with the fixed binary:
- Assistant
Pre-existing snapshot lines, not mine. Let me wait for the background test to finish:
- Assistant
Both background runs are still compiling. I'll wait for them to complete.
- Assistant
Waiting on the dependent-crate test suites (
ty_ide,ty,ty_project) to finish compiling and running — I'll report as soon as results land. - Assistant
Still compiling. Re-arming the monitor with a longer window:
- Assistant
Waiting for the test results to land.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.