Skip to content
TheChatArchiveThe Chat Archive
gpt-5.6-sol· openaiHugging 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. 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.

  3. Assistant

    ripgrep is unavailable, so I’ll use grep/find to identify the implementation and relevant tests.

  4. Assistant

    The core VarianceInferable implementations are the likely fault line. I’ll inspect variance.rs, type alias representations, and existing variance tests to see where aliases are currently collapsed to bivariant.

  5. Assistant

    Existing direct tests only query the alias object (KnownInstanceType) and already recurse into its raw value. The bug likely appears after specialization, represented as Type::TypeAlias; I’ll inspect that match arm and alias specialization/value expansion semantics.

  6. Assistant

    The alias type currently has no variance implementation of its own. I’ll locate the exhaustive Type implementation (likely in the module root) and inspect how legacy aliases are represented, since the evaluation explicitly covers both PEP 695 and legacy aliases.

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

  8. Assistant

    I need the remainder of the Type::variance_of match: that will show whether TypeAlias is grouped into the bivariant fallback and whether other wrappers need consideration.

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

  10. Assistant

    A tracked query is necessary for cycles. Before editing, I’ll reproduce the user-visible failure using the ty CLI and inspect existing variance expectations for assignment aliases; this also clarifies how “legacy generic aliases” flow through the same representation.

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

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

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

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

  15. 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 implement VarianceInferable for TypeAliasType through 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 because value_type applies specialization before recursion.

  16. Assistant

    I’ll make the focused source changes now: add a cycle-safe alias query in type_alias.rs, change Type::TypeAlias to recurse through it, and make KnownInstanceType::TypeAliasType use the same query instead of bypassing it through raw_value_type.

  17. 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, ThroughTwo is 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.

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

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

  20. 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).

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

  22. 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. Specialized Type::TypeAlias values still need the new tracked recursive query. I’ll restore the existing raw-value behavior for KnownInstanceType while retaining cycle-safe recursion for nested/specialized aliases, then rerun the test.

  23. 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_semantic library test suite.

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

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