Repository: python/mypy

Issue:
New incremental mode (-i) crasher
I've seen this a few times myself and now it's being reported. The traceback ends with
```
  File "/Users/x/src/server/.mypy/venv/lib/python3.6/site-packages/mypy/types.py", line 388, in accept
    return visitor.visit_instance(self)
  File "/Users/x/src/server/.mypy/venv/lib/python3.6/site-packages/mypy/fixup.py", line 153, in visit_instance
    node = lookup_qualified(self.modules, type_ref, self.quick_and_dirty)
  File "/Users/x/src/server/.mypy/venv/lib/python3.6/site-packages/mypy/fixup.py", line 237, in lookup_qualified
    stnode = lookup_qualified_stnode(modules, name, quick_and_dirty)
  File "/Users/x/src/server/.mypy/venv/lib/python3.6/site-packages/mypy/fixup.py", line 252, in lookup_qualified_stnode
    assert '.' in head, "Cannot find %s" % (name,)
AssertionError: Cannot find _importlib_modulespec.ModuleType
```


Discussion:
Probably this is a "leftover" that is still not fixed by https://github.com/python/mypy/pull/3203
@pkch Could you please take a look?
Oh, I'll also check if the user who reported this had a sufficiently recent mypy version.
~The user who reported this was running an older version of mypy, so there's nothing to see and nothing to do here.~

**[UPDATE: I was wrong there]**
Unfortunately, this is the recent version (after my fix). I can see from the line numbers. Is there any information about the crash (command line, whether it's intermittent, warm/cold cache, etc)?
Well, actually the user reported they got the crash after upgrading to mypy 1d1c62dc9885fedb60efb737c007aca4d93c1f37 (and they have the Terminal window logs to prove it to me :-) so I'm afraid I need to reopen this. Sorry for the quick cycling.
The user also reported: It [mypy -i] worked the first two times, then he added `# type: ignore` to the (previously empty) `__init__.py` of a very commonly used toplevel package, then it crashed.
Maybe also important, we have a script that computes the list of files to pass to mypy, and the offending `__init__.py` file (let's call it `dropbox/__init__.py`) was previously empty, so the result of adding `# type: ignore` to it was to include it in the command line. Many submodules of `dropbox` do have type annotations and are included in both runs.

Also, we run with `--follow-imports=skip --ignore-missing-imports` . This implies that the `dropbox.__init__.py` file *was not* analyzed in any way in the runs before the crash, but it *was* analyzed in the crashing run.
Also important: Python 2 mode!
> Also important: Python 2 mode!

This is the key I think.
@pkch maybe the solution is just to skip the reverse renaming in the PY2 mode?
Yup, python 2 doesn't use `_importlib_modulespec` at all. 
Do you have time to make a fix?
Yes. I can't reproduce the crash, but I'm pretty sure what @ilevkivskyi  said is sufficient. If it still crashes, we'll just have to look further.
Please send a PR -- I have a repro but I can't publish it, so I'll test your PR against it once I have it.

Resolving patch:
diff --git a/mypy/fixup.py b/mypy/fixup.py
--- a/mypy/fixup.py
+++ b/mypy/fixup.py
@@ -243,7 +243,9 @@ 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)
+    if '_importlib_modulespec' in modules:
+        # we are using python 3, so renaming is necessary
+        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
@@ -117,13 +117,18 @@
     'typing.typevar': 'typing.TypeVar',
 }
 
+# Used for python 3 only
 # Rename objects placed in _importlib_modulespec due to circular imports
+# Safe in python 2 because no one will ask for these keys
 module_rename_map = {
     '_importlib_modulespec.ModuleType': 'types.ModuleType',
     '_importlib_modulespec.ModuleSpec': 'importlib.machinery.ModuleSpec',
     '_importlib_modulespec.Loader': 'importlib.abc.Loader'
 }
 
+# Must not be used in python 2 (it will replace correct names with incorrect)
+# The check for version is currently made at the point of use
+# TODO: consider moving these maps from global scope to somewhere where python version is known
 rev_module_rename_map = {v: k for (k, v) in module_rename_map.items()}
 
 # Hard coded type promotions (shared between all Python versions).


Test patch: