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
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.
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:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /ruff, current file: none) bash-$
- Assistant
I’ll first map the repository and locate variance inference/type-alias handling, then reproduce the reported behavior with the project’s existing test tooling before making a focused fix.
- Assistant
ripgrepis unavailable, so I’ll usegrep/findto identify the implementation and relevant tests. - Assistant
The core
VarianceInferableimplementations are the likely fault line. I’ll inspectvariance.rs, type alias representations, and existing variance tests to see where aliases are currently collapsed to bivariant. - Assistant
Existing direct tests only query the alias object (
KnownInstanceType) and already recurse into its raw value. The bug likely appears after specialization, represented asType::TypeAlias; I’ll inspect that match arm and alias specialization/value expansion semantics. - Assistant
The alias type currently has no variance implementation of its own. I’ll locate the exhaustive
Typeimplementation (likely in the module root) and inspect how legacy aliases are represented, since the evaluation explicitly covers both PEP 695 and legacy aliases. - Assistant
I found the central implementations in
types.rs; I’ll inspect those match arms and nearby helper types to determine the minimal cycle-safe recursion point. - Assistant
I need the remainder of the
Type::variance_ofmatch: that will show whetherTypeAliasis grouped into the bivariant fallback and whether other wrappers need consideration. - Assistant
The defect is explicit:
Type::TypeAlias(_)returns bivariant. Replacing it with recursive value traversal is conceptually correct, but direct recursion is not itself a Salsa query, so cycles could stack-overflow. I’ll inspect the existing variance query boundaries and markdown tests to design a tracked alias-level query using the mandated bivariant cycle fallback. - Assistant
A tracked query is necessary for cycles. Before editing, I’ll reproduce the user-visible failure using the
tyCLI and inspect existing variance expectations for assignment aliases; this also clarifies how “legacy generic aliases” flow through the same representation. - Assistant
I’ll now run an isolated reproduction containing the two-deep invariant alias and reordered multi-parameter chains. Expected pre-fix behavior is failed
static_assert(not is_subtype_of(...))because the outer alias is incorrectly inferred bivariant. - Assistant
The first run was blocked by the repository’s Python 3.7 default rather than reaching variance logic. I’ll rerun with Python 3.12 and suppress the unrelated concrete-body diagnostic by using an abstract return body.
- Assistant
Alias subtyping expands the alias value directly, so my initial assertion does not exercise inferred variance. The issue manifests when an alias use determines another generic class’s inferred parameter variance. I’ll update the reproduction with wrapper classes whose members contain one-deep versus two-deep aliases.
- Assistant
Plain annotated class attributes are not an invariant use in this model, so that wrapper was an invalid probe. Inheritance explicitly calls
base_alias.variance_of, making it a precise reproduction path; I’ll switch to subclasses of the aliases. - Assistant
Despite CLI limitations around alias bases, the direct code path is confirmed: an outer alias’s raw value is
Type::TypeAlias(inner), which unconditionally returns bivariant. I’ll implementVarianceInferableforTypeAliasTypethrough a tracked helper with the existing bivariant cycle initial value, and route both specialized alias values and alias objects through it. This preserves parameter reordering becausevalue_typeapplies specialization before recursion. - Assistant
I’ll make the focused source changes now: add a cycle-safe alias query in
type_alias.rs, changeType::TypeAliasto recurse through it, and makeKnownInstanceType::TypeAliasTypeuse the same query instead of bypassing it throughraw_value_type. - Assistant
The code compiles. To verify behavior through the public interface, I’ll use a generic class where the alias appears in both producer and consumer method positions. If alias variance is preserved,
ThroughTwois invariant; if the alias is treated bivariantly, both occurrences disappear and the class becomes bivariant. I’ll also include covariance, contravariance, bivariant, and reordered multi-parameter chains. - Assistant
The implementation passed the invariant case; the two failures are from my hand-calculated reorder mapping being reversed. Expanding the aliases gives final variances
(covariant, contravariant, bivariant). I’ll correct only the temporary reproduction expectations and rerun, adding both directions where useful. - Assistant
Next I’ll verify cycle safety (self-recursive and mutually recursive aliases) and legacy assignment-style generic aliases. The cycle cases should terminate via the tracked bivariant fallback, while a direct non-cyclic occurrence can still combine with that fallback to produce a stricter result.
- Assistant
Both cycle and legacy public-interface checks pass. I’ll inspect the final diff for accidental formatting or unrelated changes, identify the exact existing unit test name, then run the focused semantic test suite (including existing recursive variance assertions).
- Assistant
The diff is minimal and contains only the intended behavior. I’ll run the focused unit test
type_alias_variance, then the crate’s full library tests if the focused run succeeds. - Assistant
The focused test exposed an important distinction: querying an unspecialized alias object must inspect
raw_value_type, because applying its default specialization erases the bound parameter identity. SpecializedType::TypeAliasvalues still need the new tracked recursive query. I’ll restore the existing raw-value behavior forKnownInstanceTypewhile retaining cycle-safe recursion for nested/specialized aliases, then rerun the test. - Assistant
The existing direct variance test now passes. I’ll rerun all temporary public-interface reproductions after that adjustment, then run the full
ty_python_semanticlibrary test suite. - Assistant
All 237 semantic library tests pass, as do the PEP 695, legacy, reordered-parameter, and recursive-cycle reproductions. I’ll do a final diff/status review and run formatting verification plus the relevant package check before submission.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →