Repository navigation
Commit 3bf4988
authored
[ty] Avoid widening compatible generic return alternatives (astral-sh#29578)
Since astral-sh#28303, when a generic call inference has multiple valid
solutions, we will intersect the return types resulting from
specializing with each valid solution. This approach has various caveats
discussed in the description of that PR. One of them is that sometimes a
call with an invariant return type (e.g. `list[T]`) can be validly
inferred as returning e.g. either `list[int]` or `list[str]`, but not
both simultaneously (consider if it actually returns `[]` -- it can be
viewed as either kind of list, but not both at the same time).
Intersecting `list[int]` and `list[str]` would result in `Never`, which
is wrong: the call can return.
For this reason, we skip intersecting return types if they vary in an
invariant type.
But in some cases one specialized return type is a subtype of all the
other ones. In this case it's safe to go ahead and prefer the narrower
type, even if they vary in invariant position and thus we wouldn't
otherwise intersect them.
This avoids a regression in astral-sh#29236.1 parent 6f06c4a commit 3bf4988
3 files changed
Lines changed: 192 additions & 14 deletions
File tree
- crates/ty_python_semantic
- resources/mdtest/generics
- legacy
- pep695
- src/types/call
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3793 | 3793 | | |
3794 | 3794 | | |
3795 | 3795 | | |
3796 | | - | |
| 3796 | + | |
| 3797 | + | |
3797 | 3798 | | |
3798 | 3799 | | |
3799 | 3800 | | |
| |||
4000 | 4001 | | |
4001 | 4002 | | |
4002 | 4003 | | |
| 4004 | + | |
| 4005 | + | |
| 4006 | + | |
| 4007 | + | |
| 4008 | + | |
| 4009 | + | |
| 4010 | + | |
| 4011 | + | |
| 4012 | + | |
| 4013 | + | |
| 4014 | + | |
| 4015 | + | |
| 4016 | + | |
| 4017 | + | |
| 4018 | + | |
| 4019 | + | |
| 4020 | + | |
| 4021 | + | |
| 4022 | + | |
| 4023 | + | |
| 4024 | + | |
| 4025 | + | |
| 4026 | + | |
| 4027 | + | |
| 4028 | + | |
| 4029 | + | |
| 4030 | + | |
| 4031 | + | |
| 4032 | + | |
| 4033 | + | |
4003 | 4034 | | |
4004 | 4035 | | |
4005 | 4036 | | |
| |||
4029 | 4060 | | |
4030 | 4061 | | |
4031 | 4062 | | |
| 4063 | + | |
| 4064 | + | |
| 4065 | + | |
| 4066 | + | |
| 4067 | + | |
| 4068 | + | |
| 4069 | + | |
| 4070 | + | |
| 4071 | + | |
| 4072 | + | |
| 4073 | + | |
| 4074 | + | |
| 4075 | + | |
| 4076 | + | |
| 4077 | + | |
| 4078 | + | |
| 4079 | + | |
| 4080 | + | |
| 4081 | + | |
| 4082 | + | |
| 4083 | + | |
| 4084 | + | |
| 4085 | + | |
| 4086 | + | |
| 4087 | + | |
| 4088 | + | |
| 4089 | + | |
| 4090 | + | |
| 4091 | + | |
| 4092 | + | |
| 4093 | + | |
| 4094 | + | |
| 4095 | + | |
| 4096 | + | |
| 4097 | + | |
| 4098 | + | |
| 4099 | + | |
| 4100 | + | |
| 4101 | + | |
| 4102 | + | |
| 4103 | + | |
| 4104 | + | |
| 4105 | + | |
| 4106 | + | |
| 4107 | + | |
| 4108 | + | |
| 4109 | + | |
| 4110 | + | |
| 4111 | + | |
4032 | 4112 | | |
4033 | 4113 | | |
4034 | 4114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1354 | 1354 | | |
1355 | 1355 | | |
1356 | 1356 | | |
1357 | | - | |
| 1357 | + | |
| 1358 | + | |
1358 | 1359 | | |
1359 | 1360 | | |
1360 | 1361 | | |
| |||
1540 | 1541 | | |
1541 | 1542 | | |
1542 | 1543 | | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
1543 | 1574 | | |
1544 | 1575 | | |
1545 | 1576 | | |
| |||
1564 | 1595 | | |
1565 | 1596 | | |
1566 | 1597 | | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
1567 | 1644 | | |
1568 | 1645 | | |
1569 | 1646 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6384 | 6384 | | |
6385 | 6385 | | |
6386 | 6386 | | |
| 6387 | + | |
| 6388 | + | |
| 6389 | + | |
| 6390 | + | |
| 6391 | + | |
| 6392 | + | |
| 6393 | + | |
| 6394 | + | |
| 6395 | + | |
| 6396 | + | |
| 6397 | + | |
| 6398 | + | |
| 6399 | + | |
| 6400 | + | |
| 6401 | + | |
| 6402 | + | |
| 6403 | + | |
| 6404 | + | |
| 6405 | + | |
| 6406 | + | |
| 6407 | + | |
| 6408 | + | |
| 6409 | + | |
| 6410 | + | |
| 6411 | + | |
| 6412 | + | |
| 6413 | + | |
| 6414 | + | |
| 6415 | + | |
| 6416 | + | |
| 6417 | + | |
| 6418 | + | |
| 6419 | + | |
6387 | 6420 | | |
6388 | 6421 | | |
6389 | 6422 | | |
| |||
6423 | 6456 | | |
6424 | 6457 | | |
6425 | 6458 | | |
6426 | | - | |
6427 | | - | |
6428 | | - | |
6429 | | - | |
6430 | | - | |
6431 | | - | |
6432 | | - | |
6433 | | - | |
6434 | | - | |
6435 | | - | |
6436 | | - | |
6437 | | - | |
6438 | 6459 | | |
6439 | 6460 | | |
6440 | 6461 | | |
| |||
0 commit comments