Skip to content

Commit

Permalink
Show more information in incompatible types in indexes (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirbyfan64 authored and gvanrossum committed Jun 3, 2016
1 parent 382d925 commit 99e9670
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
7 changes: 6 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ def incompatible_argument(self, n: int, m: int, callee: CallableType, arg_type:
if n == 1:
self.invalid_index_type(arg_type, base, context)
else:
self.fail(INCOMPATIBLE_TYPES_IN_ASSIGNMENT, context)
msg = '{} (expression has type {}, target has type {})'
arg_type_str, callee_type_str = self.format_distinctly(arg_type,
callee.arg_types[n - 1])
self.fail(msg.format(INCOMPATIBLE_TYPES_IN_ASSIGNMENT,
arg_type_str, callee_type_str),
context)
return

target = 'to {} '.format(name)
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ class C:
pass
[out]
main:3: error: Invalid index type "C" for "A"
main:4: error: Incompatible types in assignment
main:4: error: Incompatible types in assignment (expression has type "A", target has type "C")
main:5: error: Unsupported target for indexed assignment


Expand Down
4 changes: 2 additions & 2 deletions mypy/test/data/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class C:
def __add__(self, o: 'C') -> 'C': pass
[out]
main:7: error: Unsupported operand types for + ("C" and "B")
main:7: error: Incompatible types in assignment
main:7: error: Incompatible types in assignment (expression has type "B", target has type "C")
main:8: error: Invalid index type "C" for "A"

[case testOperatorAssignmentWithIndexLvalue2]
Expand Down Expand Up @@ -490,7 +490,7 @@ from typing import List
a = None # type: List[A]
b = None # type: List[int]

a[1], b[1] = a # E: Incompatible types in assignment
a[1], b[1] = a # E: Incompatible types in assignment (expression has type "A", target has type "int")
a[1], a[2] = a

class A: pass
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ def f() -> None:
x = {}
x[1] = y
g(x) # E: Argument 1 to "g" has incompatible type Dict[int, str]; expected Dict[str, int]
x[1] = 1 # E: Incompatible types in assignment
x[1] = 1 # E: Incompatible types in assignment (expression has type "int", target has type "str")
x[1] = ''
y = ''
[builtins fixtures/dict.py]
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/data/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ class B: pass
a, b = None, None # type: (A, B)
aa, bb = None, None # type: (AA, BB)

a[a], b[b] = a, bb # E: Incompatible types in assignment
a[a], b[b] = aa, b # E: Incompatible types in assignment
a[a], b[b] = a, bb # E: Incompatible types in assignment (expression has type "A", target has type "AA")
a[a], b[b] = aa, b # E: Incompatible types in assignment (expression has type "B", target has type "BB")
a[aa], b[b] = aa, bb # E: Invalid index type "AA" for "A"
a[a], b[bb] = aa, bb # E: Invalid index type "BB" for "B"
a[a], b[b] = aa, bb
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/data/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ MyDDict(dict)[0]
[out]
_program.py:6: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]
_program.py:9: error: Invalid index type "str" for "dict"
_program.py:9: error: Incompatible types in assignment
_program.py:9: error: Incompatible types in assignment (expression has type "int", target has type "str")
_program.py:19: error: List item 0 has incompatible type "Tuple[str, List[None]]"
_program.py:23: error: Invalid index type "str" for "dict"

Expand Down

0 comments on commit 99e9670

Please sign in to comment.