ISSUE DESCRIPTION Type alias statements stop tracking typevar variance after two deep…
- User
ISSUE DESCRIPTION
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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- Assistant
I'll start by exploring the repository structure and locating the variance-tracking logic for type aliases.
- Assistant
I'll inspect the variance implementation and search for type-alias handling.
- Assistant
rg is not found, let me use grep instead.
- Assistant
Let me look at the type_alias variance code and the tests more carefully.
- Assistant
Let me look at the variance inference code in generics.rs around line 2300 and the tests.
- Assistant
I need to find where type variance is inferred for type aliases.
- Assistant
The critical path is in known_instance and typevar — I'll read those next.
- Assistant
Found it:
Type::TypeAliasis hard-coded as bivariant. Checking how value types and cycle recovery work for recursive aliases. - Assistant
I'll check assignability for type aliases and look for mdtest cases covering nested aliases.
- Assistant
Let me look at the variance mdtest files carefully.
- Assistant
I'll inspect subtype relations for TypeAlias and how legacy type aliases handle variance.
- Assistant
Looking at relation.rs around line 999 - type aliases expand when checking subtype relations. But for generic type alias specialization comparison like MyInvariant[Literal[1]] vs MyInvariant[int], the...I see the core issue now: the variance inference for type aliases isn't propagating correctly through nested aliases, so when you have something like
RefRefBox[int]vsRefRefBox[str], the variance on the innerRefBoxisn't being respected, leading to bivariant behavior instead of invariant. - Assistant
Checking specialization of nested type aliases next.
- Assistant
Looking at TypeVar variance binding - BoundTypeVarInstance::variance goes through the origin (class, type alias, function) with_polarity. Let me read variance_with_polarity and binders again.
- Assistant
I'll check how binding_type resolves type aliases and find the bindspecialty for legacy ones.
- Assistant
Checking how Function and related cycle fallbacks are implemented for variance:
- Assistant
I'll check how specialized aliases appear in type alias RHS expressions and how assignability tests for nested aliases behave.
- Assistant
Looking at default赖 specialization behaviour for type aliases:
- Assistant
Let me read default_specialization:
- Assistant
How are legacy TypeAlias framesrepresented in the migr type system?
- Assistant
I'll implement VarianceInferable for TypeAliasType with salsa cycle protection, and wire Type::TypeAlias through.
- Assistant
I'll add cycle-protected variance for type aliases and route
Type::TypeAliasthrough it. - Assistant
Let me also look at ManualPEP695TypeAliasType fully and where the file ends.
- Assistant
Now I'll make the edits. For PEP695, I need to be careful - the server has #[salsa::tracked] on the whole impl. I can add another designed tracked method. I understand you're tackling the variance inference bug for PEP 695 type aliases, where nested aliases like
RefRefBoxwere incorrectly treated as bivariant. The core issue is that variance tracking isn't recursing through the alias value properly, especially with multi-level nesting and specializations. - Assistant
I'll look at fill_in_defaults quickly - for typevars without defaults it uses the typevar itself typically.
- Assistant
Let me get fill_in_defaults
- Assistant
Implementing variance for type aliases: use
value_typewhen specialized,raw_value_typewhen not, with a bivariant cycle fallback. - Assistant
Need to navigate to line 3 first, or use exact content.
- Assistant
Let me try edit with line ranges
- Assistant
Now I'll addVarianceInferable implementations after the PEP695 impl and for Manual and TypeAliasType.
- Assistant
I'll add VarianceInferable after PEP695 tracked impl (after lineأحد 148), after Manual (after 202), and after TypeAliasType methods.
- Assistant
Now add for Manual and TypeAliasType, then update types.rs and known_instance.rs
- Assistant
Now add VarianceInferable for TypeAliasType enum after its impl blockends.
- Assistant
I'll add VarianceInferable for TypeAliasType after line 340.
- Assistant
Now update Type::variance_of and KnownInstance
- Assistant
I'll update Type::variance_of to handle Type::TypeAlias
- Assistant
Update KnownInstance to use TypeAliasType.variance_of as well - that wouldæk be equivalent for unspecialized and better for specialized.
- Assistant
Next I'll extend the unit test for nested aliases and run the variance tests.
- Assistant
I won't change existing tests per the rules — next I'll run the variance unit test and pep695 mdtest to verify the fix.
- Assistant
Salsa needs
#[salsa::tracked]on the whole VarianceInferable impl — fixing that next. - Assistant
Also fix Manual
- Assistant
Rebuild and run tests.
- Assistant
I'll recreate the reported bug with a temporary mdtest and confirm the fix.
- Assistant
Nested alias test passed. Next I'll run the full variance mdtests and remove the temporary file.
- Assistant
Need to use a filter that matches both
- Assistant
Also covering type-alias and cyclic mdtests for regressions.
- Assistant
I'll verify the original transmute scenario from the issue is safe now.
- Assistant
Checking for a specialization-comparison path that depends on alias variance:
- Assistant
Looking at how specialized TypeAliases compare - they expand. But GenericAlias of something wrapping TypeAlias might use variance. I’ve reviewed the issue and the proposed fix. The core problem is that type aliases stop propagating variance correctly once you nest them two levels deep, and the current implementation falls back to bivariant too aggressively. The suggested approach—expanding the alias value and delegating variance inference through `TypeAliasType.
- Assistant
All checklist items look good. Ready to submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.