Repository: python/mypy

Issue:
"Incompatible types in assignment" for value of dict does not display the types involved
Repro:

``` python
a = {1: 1}
a[2] = ''  # E: Incompatible types in assignment
```

Compare to this simpler program:

``` python
a = 1
a = ''  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
```

"Incompatible types in assignment" for value of dict does not display the types involved
Repro:

``` python
a = {1: 1}
a[2] = ''  # E: Incompatible types in assignment
```

Compare to this simpler program:

``` python
a = 1
a = ''  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
```



Discussion:
Claiming this. Should be able to finish tomorrow!

Claiming this. Should be able to finish tomorrow!


Resolving patch:
diff --git a/mypy/messages.py b/mypy/messages.py
--- a/mypy/messages.py
+++ b/mypy/messages.py
@@ -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)


Test patch:
diff --git a/mypy/test/data/check-expressions.test b/mypy/test/data/check-expressions.test
--- a/mypy/test/data/check-expressions.test
+++ b/mypy/test/data/check-expressions.test
@@ -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
 
 
diff --git a/mypy/test/data/check-generics.test b/mypy/test/data/check-generics.test
--- a/mypy/test/data/check-generics.test
+++ b/mypy/test/data/check-generics.test
@@ -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]
@@ -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
diff --git a/mypy/test/data/check-inference.test b/mypy/test/data/check-inference.test
--- a/mypy/test/data/check-inference.test
+++ b/mypy/test/data/check-inference.test
@@ -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]
diff --git a/mypy/test/data/check-tuples.test b/mypy/test/data/check-tuples.test
--- a/mypy/test/data/check-tuples.test
+++ b/mypy/test/data/check-tuples.test
@@ -314,8 +314,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
diff --git a/mypy/test/data/pythoneval.test b/mypy/test/data/pythoneval.test
--- a/mypy/test/data/pythoneval.test
+++ b/mypy/test/data/pythoneval.test
@@ -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"