Skip to content

Commit f40dca9

Browse files
[ty] Preserve forwarded expanded-variadic diagnostic sources (#27266)
## Summary A callback using `Unpack[Config]` or `Unpack[tuple[...]]` can accept several arguments through a single `**kwargs` or `*args` declaration. Forwarded `ParamSpec` diagnostics currently fall back to the forwarding function because they cannot reliably map those arguments to the callback's source parameter: ```py import asyncio from typing import TypedDict, Unpack class Config(TypedDict): alpha: int beta: int def callback(**options: Unpack[Config]) -> None: ... async def run() -> None: await asyncio.to_thread(callback, alpha=1, beta="incorrect") ``` We now preserve parameter definitions while expanding tuple annotations, select the correct overload after `Concatenate` filtering, and map expanded arguments back to the original `*args` or `**kwargs` declaration.
1 parent 0d80497 commit f40dca9

6 files changed

Lines changed: 408 additions & 155 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/paramspec_subcall_error_location.md‎

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,11 @@ info: Function defined here
531531
| ^^^^^^^^ ---------- Parameter declared here
532532
```
533533

534-
## Overloads without parameter definitions
534+
## Overloads with expanded positional parameters
535535

536-
Expanding `Unpack[tuple[int]]` can remove the information linking an overload's parameter back to
537-
its declaration. Until that link is restored, point to the forwarding function's `*args` rather than
538-
an unrelated overload.
536+
Expanding `Unpack[tuple[int]]` must preserve the link to the callback's `*values` declaration. The
537+
diagnostic can then identify the matching overload instead of falling back to the forwarding
538+
function's `*args` parameter.
539539

540540
```py
541541
from typing import Callable, Concatenate, Unpack, overload
@@ -557,17 +557,17 @@ error[invalid-argument-type]: Argument to function `wrapper` is incorrect
557557
10 | wrapper(callback, "incorrect") # snapshot: invalid-argument-type
558558
| ^^^^^^^^^^^ Expected `int`, found `Literal["incorrect"]`
559559
info: Function defined here
560-
--> src/mdtest_snippet.py:3:5
560+
--> src/mdtest_snippet.py:7:5
561561
|
562-
3 | def wrapper[**P](callback: Callable[Concatenate[int, P], None], *args: P.args, **kwargs: P.kwargs) -> None: ...
563-
| ^^^^^^^ ------------- Parameter declared here
562+
7 | def callback(prefix: int, *values: Unpack[tuple[int]]) -> None: ...
563+
| ^^^^^^^^ --------------------------- Parameter declared here
564564
```
565565

566566
## Expanded keyword parameters
567567

568568
`Unpack[Config]` creates separate keyword parameters for `alpha` and `beta`, even though the
569-
callback declares only `**options`. Without a reliable link to that declaration, point to the
570-
forwarding function's `**kwargs` parameter.
569+
callback declares only `**options`. An error for `beta` should point to that `**options`
570+
declaration.
571571

572572
```py
573573
from typing import Callable, TypedDict, Unpack
@@ -590,10 +590,95 @@ error[invalid-argument-type]: Argument to function `wrapper` is incorrect
590590
11 | wrapper(callback, alpha=1, beta="incorrect") # snapshot: invalid-argument-type
591591
| ^^^^^^^^^^^^^^^^ Expected `int`, found `Literal["incorrect"]`
592592
info: Function defined here
593-
--> src/mdtest_snippet.py:3:5
593+
--> src/mdtest_snippet.py:9:5
594594
|
595-
3 | def wrapper[**P](callback: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
596-
| ^^^^^^^ ------------------ Parameter declared here
595+
9 | def callback(**options: Unpack[Config]) -> None: ...
596+
| ^^^^^^^^ ------------------------- Parameter declared here
597+
```
598+
599+
## Overloads with expanded keyword parameters
600+
601+
Both callback overloads unpack the same `TypedDict`, so their expanded parameters refer to the same
602+
field declarations. The diagnostic should still identify the overload selected by its `int` prefix.
603+
604+
```py
605+
from typing import Callable, Concatenate, TypedDict, Unpack, overload
606+
607+
class Config(TypedDict):
608+
value: int
609+
610+
def wrapper[**P](callback: Callable[Concatenate[int, P], None], *args: P.args, **kwargs: P.kwargs) -> None: ...
611+
@overload
612+
def callback(prefix: str, **options: Unpack[Config]) -> None: ...
613+
@overload
614+
def callback(prefix: int, **options: Unpack[Config]) -> None: ...
615+
def callback(prefix: str | int, **options: Unpack[Config]) -> None: ...
616+
617+
wrapper(callback, value="incorrect") # snapshot: invalid-argument-type
618+
```
619+
620+
```snapshot
621+
error[invalid-argument-type]: Argument to function `wrapper` is incorrect
622+
--> src/mdtest_snippet.py:13:19
623+
|
624+
13 | wrapper(callback, value="incorrect") # snapshot: invalid-argument-type
625+
| ^^^^^^^^^^^^^^^^^ Expected `int`, found `Literal["incorrect"]`
626+
info: Function defined here
627+
--> src/mdtest_snippet.py:10:5
628+
|
629+
10 | def callback(prefix: int, **options: Unpack[Config]) -> None: ...
630+
| ^^^^^^^^ ------------------------- Parameter declared here
631+
```
632+
633+
The same overload identity must survive `functools.partial`, which removes the bound prefix before
634+
forwarding the remaining arguments.
635+
636+
```py
637+
from functools import partial
638+
639+
def forward[**P](callback: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
640+
641+
forward(partial(callback, 1), value="incorrect") # snapshot: invalid-argument-type
642+
```
643+
644+
```snapshot
645+
error[invalid-argument-type]: Argument to function `forward` is incorrect
646+
--> src/mdtest_snippet.py:18:31
647+
|
648+
18 | forward(partial(callback, 1), value="incorrect") # snapshot: invalid-argument-type
649+
| ^^^^^^^^^^^^^^^^^ Expected `int`, found `Literal["incorrect"]`
650+
info: Function defined here
651+
--> src/mdtest_snippet.py:10:5
652+
|
653+
10 | def callback(prefix: int, **options: Unpack[Config]) -> None: ...
654+
| ^^^^^^^^ ------------------------- Parameter declared here
655+
```
656+
657+
## Expanded positional parameters
658+
659+
`Unpack[tuple[int, str]]` creates two positional parameters from one `*values` declaration. An error
660+
in the second argument should point to that declaration.
661+
662+
```py
663+
from typing import Callable, Unpack
664+
665+
def wrapper[**P](callback: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
666+
def callback(*values: Unpack[tuple[int, str]]) -> None: ...
667+
668+
wrapper(callback, 1, 2) # snapshot: invalid-argument-type
669+
```
670+
671+
```snapshot
672+
error[invalid-argument-type]: Argument to function `wrapper` is incorrect
673+
--> src/mdtest_snippet.py:6:22
674+
|
675+
6 | wrapper(callback, 1, 2) # snapshot: invalid-argument-type
676+
| ^ Expected `str`, found `Literal[2]`
677+
info: Function defined here
678+
--> src/mdtest_snippet.py:4:5
679+
|
680+
4 | def callback(*values: Unpack[tuple[int, str]]) -> None: ...
681+
| ^^^^^^^^ -------------------------------- Parameter declared here
597682
```
598683

599684
## Callback protocols
@@ -684,6 +769,43 @@ info: Method defined here
684769
| ^^^^^^^^ ---------- Parameter declared here
685770
```
686771

772+
## Overloaded constructors defined by __init__
773+
774+
Synthesized constructor signatures should preserve the overload selected by `Concatenate`, even when
775+
both overloads unpack the same `TypedDict` fields.
776+
777+
```py
778+
from typing import Callable, Concatenate, TypedDict, Unpack, overload
779+
780+
class Options(TypedDict):
781+
value: int
782+
783+
def wrapper[**P, T](callback: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
784+
return callback(1, *args, **kwargs)
785+
786+
class Factory:
787+
@overload
788+
def __init__(self, prefix: str, **options: Unpack[Options]) -> None: ...
789+
@overload
790+
def __init__(self, prefix: int, **options: Unpack[Options]) -> None: ...
791+
def __init__(self, prefix: str | int, **options: Unpack[Options]) -> None: ...
792+
793+
wrapper(Factory, value="incorrect") # snapshot: invalid-argument-type
794+
```
795+
796+
```snapshot
797+
error[invalid-argument-type]: Argument to function `wrapper` is incorrect
798+
--> src/mdtest_snippet.py:16:18
799+
|
800+
16 | wrapper(Factory, value="incorrect") # snapshot: invalid-argument-type
801+
| ^^^^^^^^^^^^^^^^^ Expected `int`, found `Literal["incorrect"]`
802+
info: Method defined here
803+
--> src/mdtest_snippet.py:13:9
804+
|
805+
13 | def __init__(self, prefix: int, **options: Unpack[Options]) -> None: ...
806+
| ^^^^^^^^ -------------------------- Parameter declared here
807+
```
808+
687809
## Constructors defined by a metaclass
688810

689811
A custom metaclass can determine the accepted constructor arguments through its own `__call__`. That

0 commit comments

Comments
 (0)