Skip to content

Commit 11e0d83

Browse files
committed
Only emit this diagnistic if they are actually ParamSpec parts
1 parent 6431675 commit 11e0d83

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

mypy/checkexpr.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,10 +2389,14 @@ def check_argument_count(
23892389
self.msg.too_few_arguments(callee, context, actual_names)
23902390
ok = False
23912391
elif len(mapped_args) > 1:
2392-
if actual_kinds[mapped_args[0]] == nodes.ARG_STAR:
2392+
paramspec_entries = sum(
2393+
isinstance(get_proper_type(actual_types[k]), ParamSpecType)
2394+
for k in mapped_args
2395+
)
2396+
if actual_kinds[mapped_args[0]] == nodes.ARG_STAR and paramspec_entries > 1:
23932397
self.msg.fail("ParamSpec.args should only be passed once", context)
23942398
ok = False
2395-
if actual_kinds[mapped_args[0]] == nodes.ARG_STAR2:
2399+
if actual_kinds[mapped_args[0]] == nodes.ARG_STAR2 and paramspec_entries > 1:
23962400
self.msg.fail("ParamSpec.kwargs should only be passed once", context)
23972401
ok = False
23982402
return ok

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,10 +2413,8 @@ def run2(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwar
24132413
func2(1, 2, *p) # E: Too few arguments \
24142414
# E: Argument 2 has incompatible type "int"; expected "P.args" \
24152415
# E: Argument 3 has incompatible type "*List[str]"; expected "P.args"
2416-
func2(1, *args, *p) # E: ParamSpec.args should only be passed once \
2417-
# E: Argument 3 has incompatible type "*List[str]"; expected "P.args"
2418-
func2(1, *p, *args) # E: ParamSpec.args should only be passed once \
2419-
# E: Argument 2 has incompatible type "*List[str]"; expected "P.args"
2416+
func2(1, *args, *p) # E: Argument 3 has incompatible type "*List[str]"; expected "P.args"
2417+
func2(1, *p, *args) # E: Argument 2 has incompatible type "*List[str]"; expected "P.args"
24202418
return func2(1, *args)
24212419

24222420
def run3(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.kwargs) -> T:
@@ -2466,6 +2464,14 @@ def run_bad4(func: Callable[Concatenate[int, P], T], *args: P.args, **kwargs: P.
24662464

24672465

24682466

2467+
2468+
2469+
2470+
2471+
2472+
2473+
2474+
24692475
[builtins fixtures/paramspec.pyi]
24702476

24712477
[case testOtherVarArgs]
@@ -2480,14 +2486,16 @@ def run(func: Callable[Concatenate[int, str, P], T], *args: P.args, **kwargs: P.
24802486
func2 = partial(func, **kwargs)
24812487
args_prefix: Tuple[int, str] = (1, 'a')
24822488
func2(*args_prefix) # E: Too few arguments
2483-
func2(*args, *args_prefix) # E: ParamSpec.args should only be passed once \
2484-
# E: Argument 1 has incompatible type "*P.args"; expected "int" \
2489+
func2(*args, *args_prefix) # E: Argument 1 has incompatible type "*P.args"; expected "int" \
24852490
# E: Argument 1 has incompatible type "*P.args"; expected "str" \
24862491
# E: Argument 2 has incompatible type "*Tuple[int, str]"; expected "P.args"
24872492
return func2(*args_prefix, *args)
24882493

24892494

24902495

2496+
2497+
2498+
24912499
[builtins fixtures/paramspec.pyi]
24922500

24932501
[case testParamSpecScoping]
@@ -2574,7 +2582,7 @@ reveal_type(fn(ServerErrorMiddleware)) # N: Revealed type is "__main__.Capture[
25742582

25752583
[case testRunParamSpecDuplicateArgsKwargs]
25762584
from typing_extensions import ParamSpec, Concatenate
2577-
from typing import Callable
2585+
from typing import Callable, Union
25782586

25792587
_P = ParamSpec("_P")
25802588

@@ -2593,4 +2601,12 @@ def run2(predicate: Callable[Concatenate[int, _P], None], *args: _P.args, **kwar
25932601
predicate(1, *args, **kwargs, **kwargs) # E: ParamSpec.kwargs should only be passed once
25942602
predicate(1, *args, *args, **kwargs, **kwargs) # E: ParamSpec.args should only be passed once \
25952603
# E: ParamSpec.kwargs should only be passed once
2604+
2605+
def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, **kwargs: _P.kwargs) -> None:
2606+
base_ok: tuple[int, str]
2607+
predicate(*base_ok, *args, **kwargs)
2608+
base_bad: tuple[Union[int, str], ...]
2609+
predicate(*base_bad, *args, **kwargs) # E: Argument 1 has incompatible type "*Tuple[Union[int, str], ...]"; expected "int" \
2610+
# E: Argument 1 has incompatible type "*Tuple[Union[int, str], ...]"; expected "str" \
2611+
# E: Argument 1 has incompatible type "*Tuple[Union[int, str], ...]"; expected "_P.args"
25962612
[builtins fixtures/paramspec.pyi]

0 commit comments

Comments
 (0)