Repository: python/mypy

Issue:
Internal error in check_method_override_for_base_with_name
We get an internal error in mypy 0.501 (latest from pip):
```
myfile.py:270: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.4/dist-packages/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 42, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1531, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1768, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1847, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1453, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 167, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 717, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 970, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 778, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 1063, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 601, in accept
    return visitor.visit_decorator(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 2052, in visit_decorator
    e.func.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 536, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 425, in visit_func_def
    self.check_method_override(defn)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 836, in check_method_override
    self.check_method_or_accessor_override_for_base(defn, base)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 846, in check_method_or_accessor_override_for_base
    self.check_method_override_for_base_with_name(defn, name, base)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 874, in check_method_override_for_base_with_name
    assert False, str(base_attr.node)
AssertionError: Var(config)
```

Code at the reported line:
```
    @property
    def config(self) -> Dict[str, Any]:
        """Return the merged config."""
        config = copy.deepcopy(self._driver.config)
        for name, item in self._config_overwrite.items():
            if item is not None:
                config[name] = item

        return config
```


Discussion:
Which line is line 270?
The def line. Removing the return type does not help here. 
Looks like this is in a branch where a base class defines the same name, but the type of the attribute on the base class is `None`. Not sure when that would happen.

Can you provide a small test case that reproduces the error? You'd probably need the base class of the class that triggers the error.
The baseclass defines config as property. This might trigger the error. Can try to write a test case tomorrow 
Thanks! I tried a couple of variations on that but couldn't reproduce.
Minimal "working" example:

```
from typing import Any, Dict

class A():

    def __init__(self) -> None:
        self.config = dict()    # no annotation here


class B(A):

    @property
    def config(self) -> Dict[str, Any]:
        return {"a": 7}
```
Results in this stack trace:
```
$ python3 -m mypy --show-traceback test.py 
test.py:7: error: Need type annotation for variable
test.py:12: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.4/dist-packages/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 42, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1531, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1768, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1847, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1453, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 167, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 717, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 970, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 778, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 1063, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 601, in accept
    return visitor.visit_decorator(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 2052, in visit_decorator
    e.func.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 536, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 425, in visit_func_def
    self.check_method_override(defn)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 836, in check_method_override
    self.check_method_or_accessor_override_for_base(defn, base)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 846, in check_method_or_accessor_override_for_base
    self.check_method_override_for_base_with_name(defn, name, base)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 874, in check_method_override_for_base_with_name
    assert False, str(base_attr.node)
AssertionError: Var(config)
test.py:12: note: use --pdb to drop into pdb
```

Indeed when I fix the first error `test.py:7: error: Need type annotation for variable` this turns into this:
```
$ python3 -m mypy --show-traceback test.py 
test.py:13: error: Signature of "config" incompatible with supertype "A"
```
Which I would argue should not be an error because the getter returns exactly the same type:
```
from typing import Any, Dict


class A():

    def __init__(self) -> None:
        self.config = dict()    # type: Dict[str, Any]


class B(A):

    @property
    def config(self) -> Dict[str, Any]:
        return {"a": 7}

```
But that might be another issue

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -1608,7 +1608,7 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
             partial_type = PartialType(None, name, [init_type])
         elif isinstance(init_type, Instance):
             fullname = init_type.type.fullname()
-            if (isinstance(lvalue, NameExpr) and
+            if (isinstance(lvalue, (NameExpr, MemberExpr)) and
                     (fullname == 'builtins.list' or
                      fullname == 'builtins.set' or
                      fullname == 'builtins.dict') and


Test patch:
diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test
--- a/test-data/unit/check-inference.test
+++ b/test-data/unit/check-inference.test
@@ -1267,8 +1267,22 @@ class A:
     def f(self) -> None:
         # Attributes aren't supported right now.
         self.a = [] # E: Need type annotation for variable
-        self.a.append(1) # E: Cannot determine type of 'a'
-        self.a.append('') # E: Cannot determine type of 'a'
+        self.a.append(1)
+        self.a.append('')
+[builtins fixtures/list.pyi]
+[out]
+
+[case testInferListInitializedToEmptyInClassBodyAndOverriden]
+from typing import List
+
+class A:
+    def __init__(self) -> None:
+        self.x = [] # E: Need type annotation for variable
+
+class B(A):
+    @property
+    def x(self) -> List[int]:
+        return [123]
 [builtins fixtures/list.pyi]
 [out]