Repository: python/mypy

Issue:
Incremental type check broken
This is based on my analysis of the differences of the test results with and without builtins fixtures (#3111). 

It seems that incremental type check is broken on master, unless there's something really weird with my platform.

To replicate create two files:

```
# b.py
from a import A

def f(x: A) -> None:
    x.g()
```

and

```
# a.py
class A:
    def g(self) -> None: pass
```

Then run 

```
rm -rf .mypy_cache
mypy -i b.py
touch a.py
mypy -i b.py
```

This results in the following (both on Win64 and Linux):

```
$ mypy b.py
(mypy) [pkch@kanto mypy] 2017-04-19 22:15:03 (master)
$ mypy -i b.py
(mypy) [pkch@kanto mypy] 2017-04-19 22:15:19 (master)
$ touch a.py
(mypy) [pkch@kanto mypy] 2017-04-19 22:15:23 (master)
$ mypy -i b.py
Traceback (most recent call last):
  File "/home/pkch/.virtualenvs/mypy/bin/mypy", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/mnt/c/Users/pkch/Downloads/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/main.py", line 46, in main
    res = type_check_only(sources, bin_dir, options)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/main.py", line 93, in type_check_only
    options=options)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/build.py", line 188, in build
    graph = dispatch(sources, manager)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/build.py", line 1570, in dispatch
    process_graph(graph, manager)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/build.py", line 1806, in process_graph
    process_fresh_scc(graph, prev_scc)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/build.py", line 1875, in process_fresh_scc
    graph[id].fix_cross_refs()
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/build.py", line 1341, in fix_cross_refs
    self.manager.options.quick_and_dirty)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/fixup.py", line 22, in fixup_module_pass_one
    node_fixer.visit_symbol_table(tree.names)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/fixup.py", line 81, in visit_symbol_table
    self.quick_and_dirty)
  File "/mnt/c/Users/pkch/Downloads/mypy/mypy/fixup.py", line 256, in lookup_qualified_stnode
    assert '.' in head, "Cannot find %s" % (name,)
AssertionError: Cannot find importlib.abc.Loader
(mypy) [pkch@kanto mypy] 2017-04-19 22:15:26 (master)
$
```



Discussion:
And bisect suggests it was introduced in my own PR #3107 . I'm looking into that.

Resolving patch:
diff --git a/mypy/fixup.py b/mypy/fixup.py
--- a/mypy/fixup.py
+++ b/mypy/fixup.py
@@ -15,6 +15,8 @@
 )
 from mypy.visitor import NodeVisitor
 
+from mypy.semanal import rev_module_rename_map
+
 
 def fixup_module_pass_one(tree: MypyFile, modules: Dict[str, MypyFile],
                           quick_and_dirty: bool) -> None:
@@ -248,6 +250,7 @@ def lookup_qualified(modules: Dict[str, MypyFile], name: str,
 
 def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str,
                             quick_and_dirty: bool) -> Optional[SymbolTableNode]:
+    name = rev_module_rename_map.get(name, name)
     head = name
     rest = []
     while True:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -120,6 +120,8 @@
     '_importlib_modulespec.Loader': 'importlib.abc.Loader'
 }
 
+rev_module_rename_map = {v: k for (k, v) in module_rename_map.items()}
+
 # Hard coded type promotions (shared between all Python versions).
 # These add extra ad-hoc edges to the subtyping relation. For example,
 # int is considered a subtype of float, even though there is no


Test patch: