Skip to content
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

Fine-grained: Support Python 3 unpacking expressions (and fix crash) #4966

Merged
merged 4 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
# For function calls with keyword arguments, display the argument name rather than the
# number.
arg_label = str(n)
if isinstance(context, CallExpr):
if isinstance(context, CallExpr) and len(context.arg_names) >= n:
arg_name = context.arg_names[n - 1]
if arg_name is not None:
arg_label = '"{}"'.format(arg_name)
Expand Down
84 changes: 84 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -5800,3 +5800,87 @@ class M(type):
[out]
==
a.py:2: error: Argument 1 to "f" of "M" has incompatible type "int"; expected "str"

[case testExtendedUnpacking-skip-cache]
from typing import List
from a import g
def f() -> List[int]:
a, *b = g()
return b

[file a.py]
from typing import Tuple
def g() -> Tuple[str, int, int]: pass

[file a.py.2]
from typing import Tuple
def g() -> Tuple[str, str]: pass

[builtins fixtures/tuple.pyi]
[out]
==
main:5: error: Incompatible return value type (got "List[str]", expected "List[int]")

[case testUnpackInExpression1-skip-cache]
from typing import Tuple, List
from a import t

def f() -> Tuple[int, int]:
return (1, *t())

def g() -> List[int]:
return [1, *t()]

[file a.py]
from typing import Tuple
def t() -> Tuple[int]: ...

[file a.py.2]
from typing import Tuple
def t() -> Tuple[str]: ...

[builtins fixtures/list.pyi]
[out]
==
main:5: error: Incompatible return value type (got "Tuple[int, str]", expected "Tuple[int, int]")
main:8: error: List item 1 has incompatible type "Tuple[str]"; expected "int"

[case testUnpackInExpression2-skip-cache]
from typing import Set
from a import t

def f() -> Set[int]:
return {1, *t()}

[file a.py]
from typing import Tuple
def t() -> Tuple[int]: pass

[file a.py.2]
from typing import Tuple
def t() -> Tuple[str]: pass

[builtins fixtures/set.pyi]
[out]
==
main:5: error: Argument 2 to <set> has incompatible type "*Tuple[str]"; expected "int"

[case testUnpackInExpression3-skip-cache]
from typing import Dict
from a import d

def f() -> Dict[int, str]:
return {1: '', **d()}

[file a.py]
from typing import Dict
def d() -> Dict[int, str]: pass

[file a.py.2]
from typing import Dict
def d() -> Dict[int, int]: pass

[builtins fixtures/dict.pyi]
[out]
==
main:5: error: Argument 1 to "update" of "dict" has incompatible type "Dict[int, int]"; expected "Mapping[int, str]"
11 changes: 11 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1259,3 +1259,14 @@ class B:
[out]
_testInvalidSlots.py:2: error: Incompatible types in assignment (expression has type "int", base class "object" defined the type as "Union[str, Iterable[str], None]")
_testInvalidSlots.py:4: error: Incompatible types in assignment (expression has type "Tuple[int, int]", base class "object" defined the type as "Union[str, Iterable[str], None]")

[case testDictWithStarStarSpecialCase]
from typing import Dict

def f() -> Dict[int, str]:
return {1: '', **d()}

def d() -> Dict[int, int]:
return {}
[out]
_testDictWithStarStarSpecialCase.py:4: error: Argument 1 to "update" of "dict" has incompatible type "Dict[int, int]"; expected "Mapping[int, str]"