Skip to content

Warn on invalid *args and **kwargs with ParamSpec #13892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Warn on invalid *args and **kwargs with ParamSpec
  • Loading branch information
sobolevn committed Oct 14, 2022
commit bc829f8b995cc15c3831ea4479eda49d388b6188
23 changes: 23 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ def analyze_func_def(self, defn: FuncDef) -> None:
defn.type = result
self.add_type_alias_deps(analyzer.aliases_used)
self.check_function_signature(defn)
self.check_paramspec_definition(defn.type)
if isinstance(defn, FuncDef):
assert isinstance(defn.type, CallableType)
defn.type = set_callable_name(defn.type, defn)
Expand Down Expand Up @@ -1282,6 +1283,28 @@ def check_function_signature(self, fdef: FuncItem) -> None:
elif len(sig.arg_types) > len(fdef.arguments):
self.fail("Type signature has too many arguments", fdef, blocker=True)

def check_paramspec_definition(self, func: CallableType) -> None:
param_spec_var = next(
(var for var in func.variables if isinstance(var, ParamSpecType)), None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function could have multiple ParamSpec type variables. This only picks the first one. Maybe it's enough to look at the *args and **kwargs arguments only, and ignore the variables? I.e., if either *args or **kwargs has a ParamSpec type, then we'd require that both of them are defined and they both refer to a ParamSpec (and the same ParamSpec).

)
if param_spec_var:
args = func.var_arg()
kwargs = func.kw_arg()
if args is None and kwargs is None:
return # Looks like this function does not have starred args

args_type = args.typ if args is not None else None
kwargs_type = kwargs.typ if kwargs is not None else None

if not isinstance(args_type, ParamSpecType) or not isinstance(
kwargs_type, ParamSpecType
):
self.fail(
f'ParamSpec must have "*args" typed as "{param_spec_var.name}.args" and "**kwargs" typed as "{param_spec_var.name}.kwargs"',
func,
code=codes.VALID_TYPE,
)

def visit_decorator(self, dec: Decorator) -> None:
self.statement = dec
# TODO: better don't modify them at all.
Expand Down
1 change: 1 addition & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ def visit_callable_type(self, t: CallableType, nested: bool = True) -> Type:
]
else:
arg_types = self.anal_array(t.arg_types, nested=nested)

ret = t.copy_modified(
arg_types=arg_types,
ret_type=self.anal_type(t.ret_type, nested=nested),
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1166,3 +1166,24 @@ def func3(callback: Callable[P1, str]) -> Callable[P1, str]:
return "foo"
return inner
[builtins fixtures/paramspec.pyi]


[case testInvalidDefinitions]
from typing import Callable, ParamSpec

P = ParamSpec('P')

def c1(f: Callable[P, int], *args: P.args, **kwargs: P.kwargs) -> int: ...
def c2(f: Callable[P, int]) -> int: ...

def f1(f: Callable[P, int], *args, **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f2(f: Callable[P, int], *args: P.args, **kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f3(f: Callable[P, int], *args: P.args) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f4(f: Callable[P, int], **kwargs: P.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"
def f5(f: Callable[P, int], *args, **kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P.args" and "**kwargs" typed as "P.kwargs"

# Error message test:
P1 = ParamSpec('P1')

def m1(f: Callable[P1, int], *a, **k: P1.kwargs) -> int: ... # E: ParamSpec must have "*args" typed as "P1.args" and "**kwargs" typed as "P1.kwargs"
[builtins fixtures/paramspec.pyi]