Repository: python/mypy

Issue:
Internal error on overloaded __getitem__
I was testing how to make a generic Sequence with an overloaded `__getitem__` and had these input files:

``` python
# test.py
import overload
c = overload.Container
c[10] = 20
```

``` python
# overload.py
class Container:
    def __getitem__(self, key):
        pass
```

``` python
# overload.pyi
from typing import TypeVar, Generic, Sequence, overload
T = TypeVar('T')

class Container(Generic[T], Sequence[T]):

    @overload
    def __getitem__(self, key: int) -> T: ...

    @overload
    def __getitem__(self, key: slice) -> Sequence[T]: ...
```

Running `mypy test.py` produces

```
Traceback (most recent call last):
  File "/home/mg/.virtualenvs/mypy/bin/mypy", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/mg/src/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/home/mg/src/mypy/mypy/main.py", line 50, in main
    type_check_only(sources, bin_dir, options)
  File "/home/mg/src/mypy/mypy/main.py", line 94, in type_check_only
    python_path=options.python_path)
  File "/home/mg/src/mypy/mypy/build.py", line 210, in build
    result = manager.process(initial_states)
  File "/home/mg/src/mypy/mypy/build.py", line 425, in process
    next.process()
  File "/home/mg/src/mypy/mypy/build.py", line 930, in process
    self.type_checker().visit_file(self.tree, self.tree.path)
  File "/home/mg/src/mypy/mypy/checker.py", line 409, in visit_file
    self.accept(d)
  File "/home/mg/src/mypy/mypy/checker.py", line 450, in accept
    typ = node.accept(self)
  File "/home/mg/src/mypy/mypy/nodes.py", line 564, in accept
    return visitor.visit_assignment_stmt(self)
  File "/home/mg/src/mypy/mypy/checker.py", line 1122, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
  File "/home/mg/src/mypy/mypy/checker.py", line 1167, in check_assignment
    self.check_indexed_assignment(index_lvalue, rvalue, rvalue)
  File "/home/mg/src/mypy/mypy/checker.py", line 1499, in check_indexed_assignment
    self.try_infer_partial_type_from_indexed_assignment(lvalue, rvalue)
  File "/home/mg/src/mypy/mypy/checker.py", line 1513, in try_infer_partial_type_from_indexed_assignment
    if var is not None and isinstance(var.type, PartialType):
AttributeError: 'TypeInfo' object has no attribute 'type'

*** INTERNAL ERROR ***

test.py:4: error: Internal error
```

---

Testing some more, I see the same error when I remove the overloaded `__getitem__` method and test with

``` python
# overload.pyi
from typing import TypeVar, Generic, Sequence
T = TypeVar('T')


class Container(Generic[T], Sequence[T]):
    pass
```

This is with mypy 0ec0cb406f55.

AttributeError: 'MypyFile' object has no attribute 'type'
Python 3.5.1, mypy e360dae.

Input file:

``` python
__builtins__['asd'] = 'qwe'
```

Error:

```
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/me/projects/mypy/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/Users/me/projects/mypy/mypy/main.py", line 54, in main
    type_check_only(sources, bin_dir, options)
  File "/Users/me/projects/mypy/mypy/main.py", line 98, in type_check_only
    python_path=options.python_path)
  File "/Users/me/projects/mypy/mypy/build.py", line 208, in build
    result = manager.process(initial_states)
  File "/Users/me/projects/mypy/mypy/build.py", line 408, in process
    next.process()
  File "/Users/me/projects/mypy/mypy/build.py", line 912, in process
    self.type_checker().visit_file(self.tree, self.tree.path)
  File "/Users/me/projects/mypy/mypy/checker.py", line 410, in visit_file
    self.accept(d)
  File "/Users/me/projects/mypy/mypy/checker.py", line 451, in accept
    typ = node.accept(self)
  File "/Users/me/projects/mypy/mypy/nodes.py", line 564, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/me/projects/mypy/mypy/checker.py", line 1125, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
  File "/Users/me/projects/mypy/mypy/checker.py", line 1170, in check_assignment
    self.check_indexed_assignment(index_lvalue, rvalue, rvalue)
  File "/Users/me/projects/mypy/mypy/checker.py", line 1505, in check_indexed_assignment
    self.try_infer_partial_type_from_indexed_assignment(lvalue, rvalue)
  File "/Users/me/projects/mypy/mypy/checker.py", line 1519, in try_infer_partial_type_from_indexed_assignment
    if var is not None and isinstance(var.type, PartialType):
AttributeError: 'MypyFile' object has no attribute 'type'

*** INTERNAL ERROR ***

a.py:1: error: Internal error -- please report a bug at https://github.com/JukkaL/mypy/issues
```



Discussion:
Thanks for reporting the bug! I was able to reproduce it using the latest master.

One-file repro:

```
from typing import TypeVar, Generic, Sequence

T = TypeVar('T')

class Container(Sequence[T]):
    pass

Container[10] = 20
```

Started investigating this.  This error occurs for any assignment to a subscript on an imported module.  Just subscripting a module correctly produces an error.


Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -1528,7 +1528,7 @@ def check_indexed_assignment(self, lvalue: IndexExpr,
     def try_infer_partial_type_from_indexed_assignment(
             self, lvalue: IndexExpr, rvalue: Node) -> None:
         # TODO: Should we share some of this with try_infer_partial_type?
-        if isinstance(lvalue.base, RefExpr):
+        if isinstance(lvalue.base, RefExpr) and isinstance(lvalue.base.node, Var):
             var = cast(Var, lvalue.base.node)
             if var is not None and isinstance(var.type, PartialType):
                 type_type = var.type.type


Test patch:
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
@@ -1696,3 +1696,12 @@ def f(): pass
 a = {0: [0]} if f() else {0: []}
 a() # E: Dict[int, List[int]] not callable
 [builtins fixtures/dict.py]
+
+[case testMisguidedSetItem]
+from typing import Generic, Sequence, TypeVar
+T = TypeVar('T')
+class C(Sequence[T], Generic[T]): pass
+C[0] = 0
+[out]
+main:4: error: Type expected within [...]
+main:4: error: Unsupported target for indexed assignment