Skip to content

Make sure ParamSpec suffix and arg kind do match #13468

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 3 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add some reveal_types here, as a sanity check to make sure that the typo doesn't cause mypy to make any incorrect inferences elsewhere?

Suggested change
return "foo"
reveal_type(args) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, builtins.object]"
return "foo"

Copy link
Member Author

Choose a reason for hiding this comment

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

This is not true:

main:11: note: Revealed type is "P1.args`-1"  (diff)
main:12: note: Revealed type is "P1.kwargs`-1" (diff)

Copy link
Member

Choose a reason for hiding this comment

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

This is not true:

main:11: note: Revealed type is "P1.args`-1"  (diff)
main:12: note: Revealed type is "P1.kwargs`-1" (diff)

Oh, right. Maybe this, in that case?

Suggested change
return "foo"
a = args[0]
reveal_type(a) # N: Revealed type is "builtins.object"
for b, c in kwargs.items():
reveal_type(b) # N: Revealed type is "builtins.str"
reveal_type(c) # N: Revealed type is "builtins.object"
return "foo"

Copy link
Member Author

@sobolevn sobolevn Aug 21, 2022

Choose a reason for hiding this comment

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

I think this is too much :)

No other tests do anything similar.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough :)

return inner
[builtins fixtures/paramspec.pyi]