Repository: python/mypy

Issue:
New --quick crash: "assert override is not None"
I'm seeing new crashes in quick mode in production. Note that these start with two legitimate mypy errors. On stderr:
```
dropbox/fastwalk_bridge.py:36: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.510-dev-54f1df90e40db1f0f57814a193134fa9749a2527
dropbox/fastwalk_bridge.py:36: note: use --pdb to drop into pdb
```
On stdout:
```
build_number/__init__.py:31: error: Module 'build_number._frozen_version' has no attribute 'DROPBOXEXT_VERSION'
build_number/__init__.py:102: error: "DesktopClientBuildNumber" has no attribute "name"
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/main.py", line 46, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/main.py", line 93, in type_check_only
    options=options)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/build.py", line 188, in build
    graph = dispatch(sources, manager)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/build.py", line 1595, in dispatch
    process_graph(graph, manager)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/build.py", line 1838, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/build.py", line 1931, in process_stale_scc
    graph[id].semantic_analysis()
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/build.py", line 1495, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 257, in visit_file
    self.accept(d)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 3288, in accept
    node.accept(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/nodes.py", line 749, in accept
    return visitor.visit_class_def(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 620, in visit_class_def
    defn.defs.accept(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/nodes.py", line 810, in accept
    return visitor.visit_block(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 1375, in visit_block
    self.accept(s)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 3288, in accept
    node.accept(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/nodes.py", line 631, in accept
    return visitor.visit_decorator(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 2470, in visit_decorator
    dec.func.accept(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/nodes.py", line 564, in accept
    return visitor.visit_func_def(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 376, in visit_func_def
    self.analyze_function(defn)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/semanal.py", line 550, in analyze_function
    defn.type = self.type_analyzer().visit_callable_type(defn.type, nested=False)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/typeanal.py", line 343, in visit_callable_type
    ret_type=self.anal_type(t.ret_type, nested=nested),
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/typeanal.py", line 521, in anal_type
    return t.accept(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/types.py", line 211, in accept
    return visitor.visit_unbound_type(self)
  File "/Users/---/src/desktop-client/.mypy/venv/lib/python3.6/site-packages/mypy/typeanal.py", line 210, in visit_unbound_type
    assert override is not None
AssertionError: 
```


Discussion:
This one seems easy, could you please check whether #3356 fixes this?
This crash is still occurring, so #3356 did *not* fix it.
> This crash is still occurring, so #3356 did _not_ fix it.

This looks more mysterious now. Are there any hints about how to reproduce this?
Sadly it's very shy.
This is recurring again. I really need to figure out what's going on.
If this is a big problem, I have a fix that I am not 100% happy with, but it will certainly work. The idea is very simple: replace the assert in ``--quick`` mode with a dummy ``Instance`` that will trigger errors down the way citing ``<stale cache: reconsider running mypy without --quick>`` (like we did for other situations in ``fixup.py``).
Actually, few more tracebacks will be very helpful to find a less hacky solution. Or are they all identical?
I'll mail you a few off-list.
OK, thanks for tracebacks, I now have a repro.
It involves double import of an alias, for example:
```
[file c.py]
from typing import List
Alias = List[int]

[file d.py]
from c import Alias
x: Alias

[file e.py]
from d import Alias # it is important to import from 'd', not 'c'
x: Alias
```
Now all in quick mode from cold cache:
* check ``e.py``
* modify ``c.py`` to contain ``Alias = int`` and check ``c.py``
* do any modification to ``e.py`` (just to change the hash) and check it

I will try to make a PR now.
Actually my initial solution in https://github.com/python/mypy/pull/3524#discussion_r125915947 should work (I was not sure about this at that time and didn't have a test, so I decided to postpone this to a separate PR). I will make a PR soon, just need to write the repro in a test.

Resolving patch:
diff --git a/mypy/fixup.py b/mypy/fixup.py
--- a/mypy/fixup.py
+++ b/mypy/fixup.py
@@ -88,6 +88,9 @@ def visit_symbol_table(self, symtab: SymbolTable) -> None:
                     if stnode is not None:
                         value.node = stnode.node
                         value.type_override = stnode.type_override
+                        if (self.quick_and_dirty and value.kind == TYPE_ALIAS and
+                                stnode.type_override is None):
+                            value.type_override = Instance(stale_info(), [])
                         value.alias_tvars = stnode.alias_tvars or []
                     elif not self.quick_and_dirty:
                         assert stnode is not None, "Could not find cross-ref %s" % (cross_ref,)


Test patch:
diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test
--- a/test-data/unit/check-incremental.test
+++ b/test-data/unit/check-incremental.test
@@ -2707,6 +2707,29 @@ b.x.y
 tmp/c.py:2: error: Revealed type is '<stale cache: consider running mypy without --quick>'
 tmp/c.py:5: error: "<stale cache: consider running mypy without --quick>" has no attribute "y"
 
+[case testNoCrashOnDoubleImportAliasQuick]
+# cmd: mypy -m e
+# cmd2: mypy -m c
+# cmd3: mypy -m e
+# flags: --quick
+[file c.py]
+from typing import List
+Alias = List[int]
+[file c.py.2]
+from typing import List
+Alias = int
+
+[file d.py]
+from c import Alias
+
+[file e.py]
+from d import Alias
+[file e.py.3]
+from d import Alias
+x: Alias
+[out3]
+[builtins fixtures/list.pyi]
+
 [case testSerializeAbstractPropertyIncremental]
 from abc import abstractmethod
 import typing