Skip to content

Commit 1880fdd

Browse files
committed
fix
1 parent 50fdc08 commit 1880fdd

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

mypy/checkexpr.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,19 @@ def check_argument_types(
24812481
check_arg = check_arg or self.check_arg
24822482
# Keep track of consumed tuple *arg items.
24832483
mapper = ArgTypeExpander(self.argument_infer_context())
2484+
2485+
for arg_type, arg_kind in zip(arg_types, arg_kinds):
2486+
arg_type = get_proper_type(arg_type)
2487+
if arg_kind == nodes.ARG_STAR and not self.is_valid_var_arg(arg_type):
2488+
self.msg.invalid_var_arg(arg_type, context)
2489+
if arg_kind == nodes.ARG_STAR2 and not self.is_valid_keyword_var_arg(
2490+
arg_type
2491+
):
2492+
is_mapping = is_subtype(
2493+
arg_type, self.chk.named_type("_typeshed.SupportsKeysAndGetItem")
2494+
)
2495+
self.msg.invalid_keyword_var_arg(arg_type, is_mapping, context)
2496+
24842497
for i, actuals in enumerate(formal_to_actual):
24852498
orig_callee_arg_type = get_proper_type(callee.arg_types[i])
24862499

@@ -2573,15 +2586,6 @@ def check_argument_types(
25732586
if actual_type is None:
25742587
continue # Some kind of error was already reported.
25752588
# Check that a *arg is valid as varargs.
2576-
if actual_kind == nodes.ARG_STAR and not self.is_valid_var_arg(actual_type):
2577-
self.msg.invalid_var_arg(actual_type, context)
2578-
if actual_kind == nodes.ARG_STAR2 and not self.is_valid_keyword_var_arg(
2579-
actual_type
2580-
):
2581-
is_mapping = is_subtype(
2582-
actual_type, self.chk.named_type("_typeshed.SupportsKeysAndGetItem")
2583-
)
2584-
self.msg.invalid_keyword_var_arg(actual_type, is_mapping, context)
25852589
expanded_actual = mapper.expand_actual_type(
25862590
actual_type,
25872591
actual_kind,

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ def could_not_infer_type_arguments(
13941394
self.fail("Cannot infer function type argument", context)
13951395

13961396
def invalid_var_arg(self, typ: Type, context: Context) -> None:
1397-
self.fail("List or tuple expected as variadic arguments", context)
1397+
self.fail("Expected iterable as variadic argument", context)
13981398

13991399
def invalid_keyword_var_arg(self, typ: Type, is_mapping: bool, context: Context) -> None:
14001400
typ = get_proper_type(typ)

test-data/unit/check-functools.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,16 @@ def bar(*a: bytes, **k: int):
215215
p1("a", d="a", **k)
216216
p1("a", **k) # E: Argument 2 to "foo" has incompatible type "**Dict[str, int]"; expected "str"
217217
p1(**k) # E: Argument 1 to "foo" has incompatible type "**Dict[str, int]"; expected "str"
218-
p1(*a) # E: List or tuple expected as variadic arguments
218+
p1(*a) # E: Expected iterable as variadic argument
219219

220220

221221
def baz(a: int, b: int) -> int: ...
222222
def test_baz(xs: List[int]):
223223
p3 = functools.partial(baz, *xs)
224224
p3()
225225
p3(1) # E: Too many arguments for "baz"
226+
227+
226228
[builtins fixtures/dict.pyi]
227229

228230
[case testFunctoolsPartialGeneric]

test-data/unit/check-varargs.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ class A: pass
275275

276276
a = A()
277277

278-
f(*None) # E: List or tuple expected as variadic arguments
279-
f(*a) # E: List or tuple expected as variadic arguments
278+
f(*None) # E: Expected iterable as variadic argument
279+
f(*a) # E: Expected iterable as variadic argument
280280
f(*(a,))
281281

282-
f(*4) # E: List or tuple expected as variadic arguments
283-
f(a, *4)
282+
f(*4) # E: Expected iterable as variadic argument
283+
f(a, *4) # E: Expected iterable as variadic argument
284284
[builtins fixtures/tuple.pyi]
285285

286286

@@ -545,9 +545,9 @@ if int():
545545
if int():
546546
b, b = f(b, b, *aa) # E: Argument 3 to "f" has incompatible type "*List[A]"; expected "B"
547547
if int():
548-
a, b = f(a, *a) # E: List or tuple expected as variadic arguments
548+
a, b = f(a, *a) # E: Expected iterable as variadic argument
549549
if int():
550-
a, b = f(*a) # E: List or tuple expected as variadic arguments
550+
a, b = f(*a) # E: Expected iterable as variadic argument
551551

552552
if int():
553553
a, a = f(*aa)
@@ -739,7 +739,7 @@ bar(*good1)
739739
bar(*good2)
740740
bar(*good3)
741741
bar(*bad1) # E: Argument 1 to "bar" has incompatible type "*I[str]"; expected "float"
742-
bar(*bad2) # E: List or tuple expected as variadic arguments
742+
bar(*bad2) # E: Expected iterable as variadic argument
743743
[builtins fixtures/dict.pyi]
744744

745745
-- Keyword arguments unpacking

0 commit comments

Comments
 (0)