Skip to content

Commit

Permalink
Make sure ParamSpec suffix and arg kind do match (#13468)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Aug 21, 2022
1 parent 92d3f07 commit 2756944
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,17 +838,21 @@ def anal_type_guard_arg(self, t: UnboundType, fullname: str) -> Type | None:

def anal_star_arg_type(self, t: Type, kind: ArgKind, nested: bool) -> Type:
"""Analyze signature argument type for *args and **kwargs argument."""
# TODO: Check that suffix and kind match
if isinstance(t, UnboundType) and t.name and "." in t.name and not t.args:
components = t.name.split(".")
sym = self.lookup_qualified(".".join(components[:-1]), t)
tvar_name = ".".join(components[:-1])
sym = self.lookup_qualified(tvar_name, t)
if sym is not None and isinstance(sym.node, ParamSpecExpr):
tvar_def = self.tvar_scope.get_binding(sym)
if isinstance(tvar_def, ParamSpecType):
if kind == ARG_STAR:
make_paramspec = paramspec_args
if components[-1] != "args":
self.fail(f'Use "{tvar_name}.args" for variadic "*" parameter', t)
elif kind == ARG_STAR2:
make_paramspec = paramspec_kwargs
if components[-1] != "kwargs":
self.fail(f'Use "{tvar_name}.kwargs" for variadic "**" parameter', t)
else:
assert False, kind
return make_paramspec(
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1112,3 +1112,18 @@ def func(callback: Callable[P, str]) -> Callable[P, str]:
return "foo"
return inner
[builtins fixtures/paramspec.pyi]

[case testParamSpecArgsAndKwargsMissmatch]
from typing import Callable
from typing_extensions import ParamSpec

P1 = ParamSpec("P1")

def func(callback: Callable[P1, str]) -> Callable[P1, str]:
def inner(
*args: P1.kwargs, # E: Use "P1.args" for variadic "*" parameter
**kwargs: P1.args, # E: Use "P1.kwargs" for variadic "**" parameter
) -> str:
return "foo"
return inner
[builtins fixtures/paramspec.pyi]

0 comments on commit 2756944

Please sign in to comment.