Skip to content

Commit 2b0d210

Browse files
authored
[ty] Centralize matched argument relations (#27705)
## Summary Part of astral-sh/ty#3557. This is the first focused step toward rebooting #26712 as a stack of smaller solver changes. Introduce `ArgumentRelation` as the shared representation of a matched argument/parameter pair and use it for generic specialization inference, unsatisfiable-constraint diagnostic recovery, and ordinary, positional-unpack, and keyword-unpack argument checking. Preserve effective per-element formal types, each checking path's existing actual-type lookup, synthetic receiver/source argument indices, gradual variadic filtering, and existing `TypeVarTuple` handling without changing solver behavior. ## Test plan Added mdtests covering generic bound-method diagnostics after an implicit receiver, positional and keyword source locations, homogeneous and heterogeneous unpacked generic parameters, unpacked positional element types, per-key `TypedDict` generic inference, and gradual variadic arguments that must not affect inference.
1 parent a9130f3 commit 2b0d210

3 files changed

Lines changed: 213 additions & 97 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/call/function.md‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ reveal_type(get_int()) # revealed: int
1212
## Gradual variadic parameters
1313

1414
```py
15-
from typing import Any
15+
from typing import Any, TypeVar
16+
17+
T = TypeVar("T")
1618

1719
def accepts_anything(first: int, *args: Any, **kwargs: Any) -> None: ...
1820
def accepts_only_gradual(*args: Any, **kwargs: Any) -> None: ...
21+
def preserves_first(first: T, *args: Any, **kwargs: Any) -> T:
22+
return first
1923

2024
accepts_anything(1, "one", object(), keyword=object())
2125
accepts_anything("not an int") # error: [invalid-argument-type]
2226
accepts_only_gradual(1, "one", keyword=object())
2327
accepts_only_gradual(**{1: "one"}) # error: [invalid-argument-type]
28+
29+
reveal_type(preserves_first(1, "other", keyword=object())) # revealed: Literal[1]
2430
```
2531

2632
## Object variadic parameters
@@ -1370,8 +1376,9 @@ with_default(1, 2)
13701376

13711377
### Unpacked variadic elements preserve generic bounds
13721378

1373-
Ordinary type variables are inferred from individual unpacked elements, even beside an unresolved
1374-
type-variable tuple. Their upper bounds remain enforced.
1379+
Ordinary type variables are inferred from individual unpacked elements in homogeneous or
1380+
heterogeneous tuples, even beside an unresolved type-variable tuple. Their upper bounds remain
1381+
enforced.
13751382

13761383
```toml
13771384
[environment]
@@ -1390,6 +1397,19 @@ fixed(1) # error: [invalid-argument-type]
13901397

13911398
reveal_type(suffix("prefix", "valid")) # revealed: Literal["valid"]
13921399
suffix("prefix", 1) # error: [invalid-argument-type]
1400+
1401+
def homogeneous[T: str](*args: *tuple[T, ...]) -> T:
1402+
return args[0]
1403+
1404+
reveal_type(homogeneous("first", "second")) # revealed: Literal["first", "second"]
1405+
homogeneous("valid", 1) # error: [invalid-argument-type]
1406+
1407+
def heterogeneous[T: str](*args: *tuple[int, T]) -> T:
1408+
return args[1]
1409+
1410+
def _(valid: tuple[int, str], invalid: tuple[int, int]) -> None:
1411+
reveal_type(heterogeneous(*valid)) # revealed: str
1412+
heterogeneous(*invalid) # error: [invalid-argument-type]
13931413
```
13941414

13951415
### Callable protocols enforce unpacked variadic requirements
@@ -1593,6 +1613,29 @@ f(**dict(a=1, b=2))
15931613
f(**Foo(a=1, b=2))
15941614
```
15951615

1616+
### Unpacked keyword values retain their individual generic types
1617+
1618+
Each named value in an unpacked `TypedDict` must be related to its own generic parameter instead of
1619+
using the type of the complete mapping.
1620+
1621+
```py
1622+
from typing_extensions import TypedDict, TypeVar
1623+
1624+
T = TypeVar("T")
1625+
U = TypeVar("U")
1626+
1627+
class Values(TypedDict, closed=True):
1628+
first: int
1629+
second: str
1630+
1631+
def combine(*, first: T, second: U) -> tuple[T, U]:
1632+
return first, second
1633+
1634+
values: Values = {"first": 1, "second": "value"}
1635+
1636+
reveal_type(combine(**values)) # revealed: tuple[int, str]
1637+
```
1638+
15961639
### Keyword-only parameters
15971640

15981641
```py

‎crates/ty_python_semantic/resources/mdtest/generics/pep695/functions.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,27 @@ def _(x: int):
10561056
reveal_type(C().implicit_self(x)) # revealed: tuple[C, int]
10571057
```
10581058

1059+
## Generic method errors account for the implicit receiver
1060+
1061+
An implicit `self` participates in generic inference but is absent from the call-site argument list.
1062+
Bound violations must still identify the correct positional or keyword argument.
1063+
1064+
```py
1065+
class Box:
1066+
def accept[T: int](self, value: T, *, other: T) -> T:
1067+
return value
1068+
1069+
box = Box()
1070+
1071+
reveal_type(box.accept(1, other=2)) # revealed: Literal[1, 2]
1072+
1073+
# error: 12 [invalid-argument-type] "does not satisfy upper bound `int`"
1074+
box.accept("invalid", other=1)
1075+
1076+
# error: 15 [invalid-argument-type] "does not satisfy upper bound `int`"
1077+
box.accept(1, other="invalid")
1078+
```
1079+
10591080
## `~T` is never assignable to `T`
10601081

10611082
```py

0 commit comments

Comments
 (0)