Skip to content

Accept empty list unpacking for function without parameter #7392

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

Merged
merged 4 commits into from
Aug 29, 2019
Merged
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
8 changes: 5 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,9 @@ def check_for_extra_actual_arguments(self,
for i, kind in enumerate(actual_kinds):
if i not in all_actuals and (
kind != nodes.ARG_STAR or
not is_empty_tuple(actual_types[i])):
# We accept the other iterables than tuple (including Any)
# as star arguments because they could be empty, resulting no arguments.
is_non_empty_tuple(actual_types[i])):
# Extra actual: not matched by a formal argument.
ok = False
if kind != nodes.ARG_NAMED:
Expand Down Expand Up @@ -3833,9 +3835,9 @@ def is_async_def(t: Type) -> bool:
return isinstance(t, Instance) and t.type.fullname() == 'typing.Coroutine'


def is_empty_tuple(t: Type) -> bool:
def is_non_empty_tuple(t: Type) -> bool:
t = get_proper_type(t)
return isinstance(t, TupleType) and not t.items
return isinstance(t, TupleType) and bool(t.items)


def is_duplicate_mapping(mapping: List[int], actual_kinds: List[int]) -> bool:
Expand Down
18 changes: 16 additions & 2 deletions test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class CC(C): pass
from typing import Any
d, a = None, None # type: (Any, A)
f(a, a, *d) # Fail
f(a, *d) # Fail
f(a, *d) # Ok
f(*d) # Ok

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

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

[case testVarArgsEmptyList]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that there's an existing test case that covers the empty tuple case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there is not. I thought that a tuple test is not in the scope of this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this changes the code related to handling empty/non-empty tuples as *args, it would be good to add a test case for this in case one doesn't exist already. This would validate that the change hasn't introduced a regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

from typing import List

def foo() -> None:
pass

lst: List[int] = []
foo(*lst)
[builtins fixtures/list.pyi]

[case testVarArgsEmptyTuple]
def foo() -> None:
pass

foo(*())

-- Overloads + varargs
-- -------------------
Expand Down