Repository: python/mypy

Issue:
mypy-0.641 is crashed with AssertionError: Var is lacking info
* Are you reporting a bug, or opening a feature request?

Bug report.

* Please insert below the code you are checking with mypy,
  or a mock-up repro if the source is private. We would appreciate
  if you try to simplify your case to a minimal repro.

Here: https://github.com/tk0miya/sphinx/tree/mypy_py37_bug

This is our repository. Now I'm working to migrate type annotations to py3 style.

* What is the actual behavior/output?

mypy has been crashed with following stacktrace.
```
sphinx/util/docutils.py:61: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.641
Traceback (most recent call last):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 155, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 204, in _build
    graph = dispatch(sources, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2172, in dispatch
    process_graph(graph, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2469, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2592, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 1730, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 282, in check_first_pass
    self.accept(d)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 688, in accept
    return visitor.visit_decorator(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 3002, in visit_decorator
    self.check_func_item(e.func, name=e.func.name())
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 770, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 934, in check_func_def
    self.accept(item.body)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 910, in accept
    return visitor.visit_block(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 1137, in accept
    return visitor.visit_try_stmt(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 2808, in visit_try_stmt
    self.accept(s.finally_body)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 910, in accept
    return visitor.visit_block(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 965, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1696, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1751, in check_assignment
    if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1822, in check_compatibility_all_supers
    len(lvalue_node.info.bases) > 0):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 2488, in __getattribute__
    raise AssertionError(object.__getattribute__(self, 'msg'))
AssertionError: Var is lacking info
sphinx/util/docutils.py:61: : note: use --pdb to drop into pdb
```

* What is the behavior/output you expect?

Not crashed.

* What are the versions of mypy and Python you are using?
  Do you see the same issue after installing mypy from Git master?

python 3.7.1 and 0.641.
I never tested with git master version.

* What are the mypy flags you are using? (For example --strict-optional)

Here is my setup.cfg:
```
[mypy]
show_column_numbers = True
show_error_context = True
ignore_missing_imports = True
follow_imports = skip
incremental = True
check_untyped_defs = True
warn_unused_ignores = True
strict_optional = False
```



Discussion:
Hm, this is unfortunate, sorry. Could try to find a minimalistic repro containing only one or few files?
Sorry for late response. This is minimal reproducible example. Could you check this please?
```
$ ls -lR
total 8
-rw-r--r--  1 tkomiya  staff  102 11 11 14:18 roles.py
$ cat roles.py
# -*- coding: utf-8 -*-
from docutils.parsers.rst import roles

def enable(self):
    roles.role = {}
$ mypy -V
mypy 0.641
$ mypy roles.py
roles.py:5: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.641
roles.py:5: : note: please use --show-traceback to print a traceback when reporting a bug
```
```
$ mypy --show-traceback roles.py
roles.py:5: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.641
Traceback (most recent call last):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 155, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 204, in _build
    graph = dispatch(sources, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2172, in dispatch
    process_graph(graph, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2469, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 2592, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/build.py", line 1730, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 282, in check_first_pass
    self.accept(d)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 608, in accept
    return visitor.visit_func_def(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 704, in visit_func_def
    self._visit_func_def(defn)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 708, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 770, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 934, in check_func_def
    self.accept(item.body)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 910, in accept
    return visitor.visit_block(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 965, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1696, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1751, in check_assignment
    if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/checker.py", line 1822, in check_compatibility_all_supers
    len(lvalue_node.info.bases) > 0):
  File "/Users/tkomiya/.pyenv/versions/3.7.1/lib/python3.7/site-packages/mypy/nodes.py", line 2488, in __getattribute__
    raise AssertionError(object.__getattribute__(self, 'msg'))
AssertionError: Var is lacking info
roles.py:5: : note: use --pdb to drop into pdb
```
More minimal example (as a test case):
```
[case testName]
import roles

roles.role = 1

[file roles.pyi]
def __getattr__(name): ...
```
Oh, great, it will me much easier to fix it with this repro.

Resolving patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -3017,7 +3017,7 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
                         typ = AnyType(TypeOfAny.from_error)
                 else:
                     typ = AnyType(TypeOfAny.from_error)
-                expr.kind = MDEF
+                expr.kind = GDEF
                 expr.fullname = '{}.{}'.format(file.fullname(), expr.name)
                 expr.node = Var(expr.name, type=typ)
             else:


Test patch:
diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test
--- a/test-data/unit/check-modules.test
+++ b/test-data/unit/check-modules.test
@@ -2627,3 +2627,53 @@ import foo.bar.baz
 reveal_type(foo.bar.baz.x)  # E: Revealed type is 'builtins.int'
 [file foo/bar/baz.py]
 x = 0
+
+[case testModuleGetAttrAssignUnannotated]
+import roles
+# this should not crash
+roles.role = 1
+
+[file roles.pyi]
+def __getattr__(name): ...
+
+[case testModuleGetAttrAssignUnannotatedDouble]
+import roles
+# this also should not crash
+roles.role.attr = 1
+
+[file roles.pyi]
+def __getattr__(name): ...
+
+[case testModuleGetAttrAssignAny]
+import roles
+roles.role = 1
+
+[file roles.pyi]
+from typing import Any
+def __getattr__(name: str) -> Any: ...
+
+[case testModuleGetAttrAssignError]
+import roles
+
+roles.role = 1  # E: Incompatible types in assignment (expression has type "int", variable has type "str")
+
+[file roles.pyi]
+def __getattr__(name: str) -> str: ...
+
+[case testModuleGetAttrAssignSubmodule]
+import roles
+roles.role = 1
+roles.missing.attr = 1
+
+[file roles/__init__.pyi]
+from typing import Any
+def __getattr__(name: str) -> Any: ...
+
+[case testModuleGetAttrAssignSubmoduleStrict]
+import roles
+roles.role = 1  # E: Incompatible types in assignment (expression has type "int", variable has type Module)
+
+[file roles/__init__.pyi]
+from types import ModuleType
+def __getattr__(name: str) -> ModuleType: ...
+[builtins fixtures/module.pyi]