Skip to content
TheChatArchiveThe Chat Archive
fable-5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

Type alias statements stop tracking typevar variance after two deep Summary Ran into…

  1. 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.

  2. Assistant

    The repro passed — the subtype check must resolve through the alias value. Let me find where the alias typevar's variance actually matters.

  3. Assistant

    The static asserts pass since relations unwrap alias values. Let me find where the alias typevar variance is user-observable — likely hover/display and the referenced issue.

  4. Assistant

    Now let me understand how legacy type aliases bind typevars:

  5. Assistant

    Let me look at the GenericAlias variance implementation to mirror the pattern:

  6. Assistant

    Now I'll implement. First check imports in known_instance.rs and the top of type_alias.rs:

  7. Assistant

    Now implement the fix in type_alias.rs:

  8. Assistant

    Now add the ManualPEP695TypeAliasType and TypeAliasType impls:

  9. Assistant

    Now update the KnownInstanceType arm and the Type::variance_of arm:

  10. Assistant

    The test catches the bug. Restore the fix:

  11. Assistant

    Now the legacy alias case. Let me check how legacy typevar variance flags are inferred:

  12. Assistant

    Unit tests pass. Now add hover tests in ty_ide for the two-deep case (the issue's observable):

  13. Assistant

    The hover now shows the correct variance. Fill in the inline snapshots:

  14. Assistant

    Now run the broader test suites to check for regressions:

  15. Assistant

    Now verify the original issue scenario end-to-end, including recursion termination:

  16. Assistant

    All green. Let me review the complete diff:

  17. Assistant

    The diff is complete. Now run prek on the changed files per repo guidelines:

  18. Assistant

    uvx/prek aren't available in this environment — falling back to cargo fmt for the Rust formatting hook:

  19. Assistant

    These are artifacts of the earlier resource-exhausted parallel run (thread-pool creation failed), not real diffs — the same tests passed with --test-threads=4. Cleaning them up:

  20. Assistant

    Done. Variance inference now recurses through type alias values instead of treating alias uses as bivariant.

    The bug

    BoundTypeVarInstance::variance_of recursion had two holes for type aliases:

    1. Type::TypeAlias(_) was in the "always bivariant" bucket in Type::variance_of (crates/ty_python_semantic/src/types.rs). Since a PEP 695 alias's RHS referencing another alias (type RefRefBox[T] = RefBox[T]) infers to Type::TypeAlias(RefBox[T@RefRefBox]), the typevar was never found — hence "bivariant after two deep."
    2. KnownInstanceType::TypeAliasType only looked at raw_value_type, ignoring the specialization. This broke even one level deep for legacy implicit aliases (ImplicitAlias = CovariantAlias[LegacyT]), whose binding type is a specialized TypeAliasType — the queried typevar lives in the specialization, not the raw RHS.

    The fix (crates/ty_python_semantic/src/types/type_alias.rs)

    • Added a salsa-tracked VarianceInferable impl for PEP695TypeAliasType with the existing cycle_initial = Bivariant fallback (same pattern as GenericAlias/StaticClassLiteral/FunctionType):
      • unspecialized → recurse into raw_value_type (where the alias's own typevars still occur);
      • specialized → compose each alias typevar's definition-site variance with the use-site variance of the queried typevar in the corresponding specialized type, mirroring GenericAlias::variance_of.
    • Added a tracked impl for ManualPEP695TypeAliasType (recurses into value_type) and a dispatching impl for the TypeAliasType enum.
    • Type::TypeAlias(alias) and KnownInstanceType::TypeAliasType now both route through it.

    Recursive/mutually recursive aliases terminate via salsa cycle recovery with the bivariant fallback (verified: direct cycles, and lazy cycles like type A[T] = list[B[T]] | None; type B[T] = list[A[T]] | None).

    Tests

    • types::tests::type_alias_variance extended with: 2- and 3-deep chains for all four variances, a reordered two-parameter chain (type Swapped[T, U] = CoContraAlias[U, T] → contravariant/covariant correctly swapped), direct and lazy mutual recursion (bivariant, terminates), and legacy implicit aliases of a generic PEP 695 alias. I verified the new assertions fail (Bivariant instead of Covariant) on the unfixed code.
    • Hover tests in ty_ide for the issue's observable: T@ListAlias (invariant) and T@TupleAliasAlias (covariant) through 2- and 3-level chains.
    • Full ty_python_semantic (338 mdtests + lib), ty_ide (1487), ty_project, ty CLI, and ty_server suites pass; cargo clippy --workspace --all-targets --all-features -- -D warnings and cargo fmt --check are clean. (uvx prek isn't available in this environment, so I ran rustfmt/clippy directly.)

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