Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Type alias statements stop tracking typevar variance after two deep…

  1. 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. Claude Assistant

    Now let's look at the class variance code and the tracked query with cycle handling.

  3. Claude Assistant

    Now add the VarianceInferable impls after the PEP695TypeAliasType impl block.

  4. Claude Assistant

    Now add the ManualPEP695TypeAliasType variance query and the TypeAliasType dispatch.

  5. Claude Assistant

    Now add the TypeAliasType dispatch impl at the end of its impl block.

  6. Claude Assistant

    Now update known_instance.rs and types.rs.

  7. Claude Assistant

    Now update types.rs for the Type::TypeAlias arm.

  8. Claude Assistant

    Now run the mdtest suite (the big one) and the ty_ide tests.

  9. Claude Assistant

    I've completed the fix. Here's my summary and the checklist review:

    Change summary

    Root cause: Type::TypeAlias(_) (a type alias appearing inside a type expression, e.g. RefBox[T] in type RefRefBox[T] = RefBox[T]) was unconditionally treated as Bivariant by variance inference, so any typevar that only reached a class through a nested alias lost its variance.

    Fix (3 files):

    • types/type_alias.rs:
      • New tracked query PEP695TypeAliasType::unspecialized_variance_of (cycle_initial → Bivariant) which recurses into raw_value_type. It's always invoked on the unspecialized alias, so the set of possible cycle participants is finite and Salsa's bivariant cycle fallback terminates (mutually) recursive aliases (including "growing" ones like type Growing[T] = Growing[list[T]] | None).
      • VarianceInferable for PEP695TypeAliasType: for a specialized alias it mirrors GenericAlias::variance_of — composing the variance of the queried typevar in each substituted type with the variance of the corresponding alias typevar in the alias body (this handles reordered/multi-parameter alias chains).
      • ManualPEP695TypeAliasType (legacy TypeAliasType(...) constructor aliases) gets an analogous tracked, cycle-safe variance_of, plus a TypeAliasType enum dispatch impl.
    • types/known_instance.rs: KnownInstanceType::TypeAliasType now delegates to the new (specialization-aware, cycle-safe) alias implementation.
    • types.rs: Type::TypeAlias(alias) now recurses instead of returning Bivariant.

    Checklist

    • Reproduced the reported error: Yes. Subtyping asserts don't observe alias variance (aliases are expanded), so I reproduced at the semantic level with a temporary unit test using the same entry point as the existing type_alias_variance test: Cov2/Inv2/Contra2 (two-deep aliases) returned Bivariant before the fix (confirmed by stashing my source changes: left: Bivariant, right: Covariant).
    • Confirmed the fix resolves it: Yes. With the fix, one/two/three-level chains yield Covariant/Contravariant/Invariant/Bivariant correctly, contravariant-of-contravariant flips to Covariant, and a reordered two-parameter chain (type Two2[T, U] = Two1[U, T]) gives the swapped variances.
    • Edge cases: recursive (type Rec[T] = None | list[Rec[T]] → Bivariant), partially recursive (Rec2 → Invariant), mutually recursive (G/H → Bivariant), unboundedly "growing" alias specializations (Growing[list[T]] → Bivariant, terminates), bare/unspecialized nested aliases, explicit-variance typevars, and manual TypeAliasType(...) aliases (including recursive ones) were all exercised in temporary tests and terminate with sensible results.
    • Existing tests pass: cargo test -p ty_python_semantic --lib (203 passed, incl. type_alias_variance), --test mdtest (338 passed), --test corpus, cargo test -p ty_ide (1487 passed), cargo test -p ty, plus cargo clippy --all-targets, cargo check --workspace --all-targets and cargo fmt --check. All temporary test code was removed (git status shows only the three source files modified).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.