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
I’m using the repository’s working-on-ty skill because this is a ty semantic inference bug. I’ll trace the alias variance path, add focused regression cases for modern and legacy aliases, then run the narrow tests and hooks.
- Assistant
The worktree is clean. Ripgrep isn’t installed in this environment, so I’m switching to repository-local search tools while keeping the investigation scoped to variance inference.
- Assistant
The failure is at the exact handoff between aliases:
Type::variance_ofcurrently returns bivariant for every resolvedType::TypeAlias, so the first alias’s RHS is analyzed but the next alias ends recursion. I’m consolidating alias variance into a cycle-aware query that evaluates the specialized alias value. - Assistant
The implementation compiles through the semantic crate so far. I’m expanding the existing unit test to cover three-level chains, every variance, parameter reordering, and mutual recursion; these assertions inspect inferred variance directly rather than relying only on downstream subtype behavior.
- Assistant
The focused legacy mdtest passes, including covariant, contravariant, invariant, and bivariant three-level chains. No snapshots or pending snapshots were produced. The direct unit test also passes its reordered multi-parameter and cyclic cases; I’m moving on to the full semantic-crate suite.
- Assistant
The full
ty_python_semanticsuite passes: 237 unit tests, 338 mdtests, and doctests. The required hook runner can’t start becauseuvxis not installed in this environment; I’m checking for an equivalent local hook command and will still run the available static checks.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.