Commit c5b4ee6
authored
[ty] Support solving generics involving PEP 695 type aliases (#22678)
## Summary
Fixes type variable inference when a PEP 695 type alias is used as a
function parameter.
**Before:**
```python
type MyList[T] = list[T]
def head[T](my_list: MyList[T]) -> T: ...
reveal_type(head([1, 2])) # Unknown
```
**After:** Correctly infers `int` by expanding `MyList[T]` to `list[T]`
during solving.
Changes in `generics.rs`:
- **Early formal expansion**: Expand parameter type aliases immediately
so underlying structure is visible for structural matching
- **Late actual expansion**: Expand argument type aliases after direct
matching attempts to preserve alias identity for `reveal_type()` and
literal promotion
- **Union induction**: Recursively match formal type against each union
element to support nested aliases like `ListOfPairs[T] = list[Pair[T]]`
The early/late expansion asymmetry is intentional: formal needs
structural visibility; actual needs identity preservation.
## Test Plan
- New tests in `aliases.md` covering simple, nested, and union alias
cases
- All 313 existing mdtests pass
Closes astral-sh/ty#1851.1 parent b9a6129 commit c5b4ee6
2 files changed
Lines changed: 49 additions & 0 deletions
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1769 | 1769 | | |
1770 | 1770 | | |
1771 | 1771 | | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
1772 | 1778 | | |
1773 | 1779 | | |
1774 | 1780 | | |
| |||
2100 | 2106 | | |
2101 | 2107 | | |
2102 | 2108 | | |
| 2109 | + | |
| 2110 | + | |
| 2111 | + | |
| 2112 | + | |
| 2113 | + | |
| 2114 | + | |
| 2115 | + | |
| 2116 | + | |
| 2117 | + | |
2103 | 2118 | | |
2104 | 2119 | | |
2105 | 2120 | | |
| |||
0 commit comments