Skip to content

Commit 8954045

Browse files
onlinedJukkaL
authored andcommitted
Accept empty list unpacking for function without parameter (#7392)
Also assume `Any` as `*args` could be empty. Fixes #4997.
1 parent d2a7f05 commit 8954045

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,9 @@ def check_for_extra_actual_arguments(self,
12351235
for i, kind in enumerate(actual_kinds):
12361236
if i not in all_actuals and (
12371237
kind != nodes.ARG_STAR or
1238-
not is_empty_tuple(actual_types[i])):
1238+
# We accept the other iterables than tuple (including Any)
1239+
# as star arguments because they could be empty, resulting no arguments.
1240+
is_non_empty_tuple(actual_types[i])):
12391241
# Extra actual: not matched by a formal argument.
12401242
ok = False
12411243
if kind != nodes.ARG_NAMED:
@@ -3836,9 +3838,9 @@ def is_async_def(t: Type) -> bool:
38363838
return isinstance(t, Instance) and t.type.fullname() == 'typing.Coroutine'
38373839

38383840

3839-
def is_empty_tuple(t: Type) -> bool:
3841+
def is_non_empty_tuple(t: Type) -> bool:
38403842
t = get_proper_type(t)
3841-
return isinstance(t, TupleType) and not t.items
3843+
return isinstance(t, TupleType) and bool(t.items)
38423844

38433845

38443846
def is_duplicate_mapping(mapping: List[int], actual_kinds: List[int]) -> bool:

test-data/unit/check-varargs.test

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class CC(C): pass
353353
from typing import Any
354354
d, a = None, None # type: (Any, A)
355355
f(a, a, *d) # Fail
356-
f(a, *d) # Fail
356+
f(a, *d) # Ok
357357
f(*d) # Ok
358358

359359
g(*d)
@@ -366,7 +366,6 @@ class A: pass
366366
[builtins fixtures/list.pyi]
367367
[out]
368368
main:3: error: Too many arguments for "f"
369-
main:4: error: Too many arguments for "f"
370369

371370
[case testListVarArgsAndSubtyping]
372371
from typing import List
@@ -448,6 +447,21 @@ def f(x: int, *, y: str) -> None: pass
448447
f(y='x', *(1,))
449448
[builtins fixtures/list.pyi]
450449

450+
[case testVarArgsEmptyList]
451+
from typing import List
452+
453+
def foo() -> None:
454+
pass
455+
456+
lst: List[int] = []
457+
foo(*lst)
458+
[builtins fixtures/list.pyi]
459+
460+
[case testVarArgsEmptyTuple]
461+
def foo() -> None:
462+
pass
463+
464+
foo(*())
451465

452466
-- Overloads + varargs
453467
-- -------------------

0 commit comments

Comments
 (0)