Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,16 @@ def pack_paramspec_args(self, an_args: Sequence[Type]) -> list[Type]:
# These do not support mypy_extensions VarArgs, etc. as they were already analyzed
# TODO: should these be re-analyzed to get rid of this inconsistency?
count = len(an_args)
if count > 0:
first_arg = get_proper_type(an_args[0])
if not (count == 1 and isinstance(first_arg, (Parameters, ParamSpecType, AnyType))):
return [Parameters(an_args, [ARG_POS] * count, [None] * count)]
return list(an_args)
if count == 0:
return []
if count == 1 and isinstance(get_proper_type(an_args[0]), AnyType):
# Single Any is interpreted as ..., rather that a single argument with Any type.
# I didn't find this in the PEP, but it sounds reasonable.
return list(an_args)
if any(isinstance(a, (Parameters, ParamSpecType)) for a in an_args):
# Nested parameter specifications are not allowed.
return list(an_args)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Another option might be to generate an error and return a list with a single any type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, currently this will say something like "Too many arguments", I will check if we can make error messages better on this.

return [Parameters(an_args, [ARG_POS] * count, [None] * count)]

def cannot_resolve_type(self, t: UnboundType) -> None:
# TODO: Move error message generation to messages.py. We'd first
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1741,3 +1741,16 @@ def bar(x): ...

reveal_type(bar) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.float, def (x: builtins.str) -> builtins.str)"
[builtins fixtures/paramspec.pyi]

[case testParamSpecDecoratorOverloadNoCrashOnInvalidTypeVar]
from typing import Any, Callable, List
from typing_extensions import ParamSpec

P = ParamSpec("P")
T = 1

Alias = Callable[P, List[T]] # type: ignore
def dec(fn: Callable[P, T]) -> Alias[P, T]: ... # type: ignore
f: Any
dec(f) # No crash
[builtins fixtures/paramspec.pyi]