[ty] Fix bidirectional inference with PEP 695 union type aliases#22988
Merged
[ty] Fix bidirectional inference with PEP 695 union type aliases#22988
Conversation
Typing conformance resultsNo changes detected ✅ |
|
When a PEP 695 type alias that expands to a union (like `type MaybeList[T] = list[T] | T`) is used as a type annotation, bidirectional type inference was failing to infer the correct specialization for generic calls. The issue was in `ArgumentTypeChecker::infer_specialization` where the type context was used directly without expanding type aliases. When the type context is a `Type::TypeAlias`, the `filter_union` and `class_specialization` calls would fail because the type alias wasn't being expanded to its value type first. The fix expands type aliases to their value types before using them for specialization inference, allowing union members to be correctly filtered and matched. Fixes astral-sh/ty#2682 https://claude.ai/code/session_01XvWDzFJcmPfrXQRJdmpMBb
6bb4b87 to
08887a6
Compare
ibraheemdev
approved these changes
Jan 30, 2026
Member
ibraheemdev
left a comment
There was a problem hiding this comment.
It might be worth checking other uses of filter_union to see if we rely on type aliases being expanded. Ideally filter_union would expand type aliases internally if needed, but I think that would break the current generics solver.
Contributor
Author
|
Yeah, looking into other uses of |
carljm
pushed a commit
that referenced
this pull request
Jan 30, 2026
This change modifies `filter_union` to resolve type aliases before checking if the type is a union. This allows `filter_union` to properly handle cases where a type alias resolves to a union type. With this change, the explicit `resolve_type_alias` call in `bind.rs` (added in #22988) is now redundant and has been removed. Added additional test cases for union type aliases with: - TypedDict dict literal inference - TypedDict dict() call inference - Set literal inference https://claude.ai/code/session_01Fsp2dSw6mnTBV1M2Fnf6gt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes astral-sh/ty#2682
Fix bidirectional type inference when PEP 695 type aliases expand to union types. Previously, when a type alias like
type MaybeList[T] = list[T] | Twas used as a type annotation, the specialization inference would fail to correctly infer type parameters from the assigned value.The issue was that type aliases were not being expanded to their value types before filtering union members for specialization inference. This prevented the type checker from identifying class instances within the expanded union that could be used to infer generic type parameters.
The fix expands type aliases to their underlying value types before performing the union filtering and reverse type inference. This allows bidirectional inference to work correctly with union type aliases while maintaining proper variance handling.
Test Plan
Added test cases in
crates/ty_python_semantic/resources/mdtest/generics/pep695/aliases.md.