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
I'll investigate how ty tracks typevar variance through type aliases and fix the recursion depth issue. Starting with the ty skill and relevant code.
- Assistant
Reproducing the bug, then implementing the fix.
- Assistant
The bug is confirmed:
RefRefBoxincorrectly allows the transmute. Implementing the fix. - Assistant
Adding tests for nested alias variance:
- Assistant
TypeAliasTypeisn't a tracked Salsa type, so I'll addvariance_ofon the underlying tracked alias types instead. - Assistant
Unspecialized aliases should use
raw_value_typefor variance; only specialized aliases needvalue_type. - Assistant
Fixing the reordered-parameter test — my
dict[V, K]variance expectations were wrong. - Assistant
The fix works — all three transmute functions now correctly error. Checking lints and running a broader test.
- Assistant
Fixed. Nested PEP 695 type aliases were losing variance after one hop because
Type::TypeAliaswas treated as bivariant instead of recursing into the alias value.Root cause
In
Type::variance_of,Type::TypeAlias(_)fell through to the defaultBivariantbranch. That’s fine for a one-level alias (type RefBox[T] = Box[T]), which goes throughKnownInstanceType::TypeAliasTypeand recurses correctly. But the RHS of a second-level alias (type RefRefBox[T] = RefBox[T]) is represented asType::TypeAlias, so variance inference stopped there.Fix
- Added tracked
variance_ofonPEP695TypeAliasTypeandManualPEP695TypeAliasTypewith the same bivariant cycle fallback used for classes and functions. - Routed
Type::TypeAliasthroughTypeAliasType::variance_ofinstead of returning bivariant. - Used
raw_value_typefor unspecialized aliases andvalue_typefor specialized ones, so top-level inference stays correct while nested chains get the right substitution.
Tests
- Added tracked
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.