Skip to content

Improve error message for callables with invalid Concatenate #13529

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,9 @@ def analyze_callable_args_for_paramspec(
def analyze_callable_args_for_concatenate(
self, callable_args: Type, ret_type: Type, fallback: Instance
) -> CallableType | None:
"""Construct a 'Callable[C, RET]', where C is Concatenate[..., P], returning None if we
cannot.
"""Construct a 'Callable[C, RET]', where C is Concatenate[..., P]

Return `None` if we cannot.
"""
if not isinstance(callable_args, UnboundType):
return None
Expand All @@ -1039,7 +1040,17 @@ def analyze_callable_args_for_concatenate(
if sym.node.fullname not in ("typing_extensions.Concatenate", "typing.Concatenate"):
return None

tvar_def = self.anal_type(callable_args, allow_param_spec=True)
tvar_def = get_proper_type(self.anal_type(callable_args, allow_param_spec=True))
if isinstance(tvar_def, AnyType) and tvar_def.type_of_any == TypeOfAny.from_error:
# Some error happened, we won't be able to construct a proper type anyway.
# So, instead return a callable that accepts anything.
return CallableType(
arg_names=[None] * 2,
arg_types=[AnyType(TypeOfAny.from_error)] * 2,
arg_kinds=[ARG_STAR, ARG_STAR2],
ret_type=AnyType(TypeOfAny.from_error),
fallback=fallback,
)
if not isinstance(tvar_def, ParamSpecType):
return None

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 @@ -680,6 +680,27 @@ def f(x: int) -> None: ...
n.foo(f)
[builtins fixtures/paramspec.pyi]

[case testLastParameterToConcatenateAndInvalidCallable]
# See https://github.com/python/mypy/issues/13518
from typing_extensions import Concatenate, ParamSpec
from typing import Callable, TypeVar, Any

P = ParamSpec("P")

def f(fn: Callable[Concatenate[P, int], None]): ... # E: The last parameter to Concatenate needs to be a ParamSpec
reveal_type(f) # N: Revealed type is "def [P] (fn: def (*Any, **Any) -> Any) -> Any"
[builtins fixtures/paramspec.pyi]

[case testUnboundParamSpecInConcatenateWithAlias]
from typing import Callable
from typing_extensions import Concatenate, ParamSpec

P = ParamSpec('P')

X = Callable[Concatenate[int, P], None] # E: The last parameter to Concatenate needs to be a ParamSpec \
# E: ParamSpec "P" is unbound
[builtins fixtures/paramspec.pyi]

[case testParamSpecLiteralsTypeApplication]
from typing_extensions import ParamSpec
from typing import Generic, Callable
Expand Down