Skip to content

Commit 822b12a

Browse files
committed
Fix crashing on invalid Concatenate usage
1 parent 94838b0 commit 822b12a

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

mypy/typeanal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,15 @@ def visit_callable_type(
11991199
"Arguments not allowed after ParamSpec.args", t, code=codes.VALID_TYPE
12001200
)
12011201
at = self.anal_type(ut, nested=nested, allow_unpack=False)
1202+
if isinstance(at, Parameters):
1203+
# A Parameters here comes from a misplaced Concatenate ending in
1204+
# an explicit ellipsis, e.g. Callable[[Concatenate[int, ...]], None].
1205+
self.fail(
1206+
"Concatenate is only valid as the first argument to Callable",
1207+
ut,
1208+
code=codes.VALID_TYPE,
1209+
)
1210+
at = AnyType(TypeOfAny.from_error)
12021211
arg_types.append(at)
12031212

12041213
if nested and arg_types:

test-data/unit/check-parameter-specification.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,3 +2755,16 @@ reveal_type(Sneaky(f8, 1, y='').kwargs) # N: Revealed type is "builtins.dict[bu
27552755
reveal_type(Sneaky(f9, 1, y=0).kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str})"
27562756
reveal_type(Sneaky(f9, 1, y=0, z='').kwargs) # N: Revealed type is "TypedDict('builtins.dict', {'x'?: builtins.int, 'y': builtins.int, 'z'?: builtins.str})"
27572757
[builtins fixtures/paramspec.pyi]
2758+
2759+
[case testParamSpecNoCrashOnConcatenateInCallableArg]
2760+
from typing import Callable
2761+
from typing_extensions import Concatenate, ParamSpec
2762+
2763+
P = ParamSpec("P")
2764+
2765+
def takes_paramspec(f: Callable[P, object]) -> bool:
2766+
return False
2767+
2768+
def run(callback: Callable[[Concatenate[int, ...]], None]) -> None: # E: Concatenate is only valid as the first argument to Callable
2769+
takes_paramspec(callback)
2770+
[builtins fixtures/paramspec.pyi]

0 commit comments

Comments
 (0)