# Buggy code
def add_symbol(self, name: str, node: SymbolTableNode, context: Context) -> None:
    # NOTE: This is closely related to SemanticAnalyzerPass2.add_symbol. Since both methods
    #     will be called on top-level definitions, they need to co-operate. If you change
    #     this, you may have to change the other method as well.
    if self.sem.is_func_scope():
        assert self.sem.locals[-1] is not None
        if name in self.sem.locals[-1]:
            # Flag redefinition unless this is a reimport of a module.
            if not (
                node.kind == MODULE_REF and self.sem.locals[-1][name].node == node.node
            ):
                self.sem.name_already_defined(name, context, self.sem.locals[-1][name])
        self.sem.locals[-1][name] = node
    else:
        assert self.sem.type is None  # Pass 1 doesn't look inside classes
        existing = self.sem.globals.get(name)
        if (
            existing
            and (not isinstance(node.node, MypyFile) or existing.node != node.node)
            and existing.kind != UNBOUND_IMPORTED
            and not isinstance(existing.node, ImportedName)
        ):
            # Modules can be imported multiple times to support import
            # of multiple submodules of a package (e.g. a.x and a.y).
            ok = False
            # Only report an error if the symbol collision provides a different type.
            if existing.type and node.type and is_same_type(existing.type, node.type):
                ok = True
            if not ok:
                self.sem.name_already_defined(name, context, existing)
        elif not existing:
            self.sem.globals[name] = node


# Traceback (AssertionError)
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/__main__.py", line 11, in <module>
main(None)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/main.py", line 91, in main
res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/main.py", line 148, in type_check_only
fscache=fscache)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 177, in build
flush_errors, fscache)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 350, in _build
graph = dispatch(sources, manager)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 2560, in dispatch
process_graph(graph, manager)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 2846, in process_graph
process_fresh_modules(graph, prev_scc, manager)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 2932, in process_fresh_modules
graph[id].fix_cross_refs()
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/build.py", line 1975, in fix_cross_refs
self.options.use_fine_grained_cache)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/fixup.py", line 24, in fixup_module
node_fixer.visit_symbol_table(tree.names)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/fixup.py", line 89, in visit_symbol_table
value.node.accept(self)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/nodes.py", line 2539, in accept
return visitor.visit_type_alias(self)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/fixup.py", line 135, in visit_type_alias
a.target.accept(self.type_fixer)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/types.py", line 511, in accept
return visitor.visit_instance(self)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/fixup.py", line 149, in visit_instance
inst.type = lookup_qualified_typeinfo(self.modules, type_ref, self.quick_and_dirty)
File "/Users/kevin/coding/mypy-bug/venv/lib/python3.7/site-packages/mypy/fixup.py", line 239, in lookup_qualified_typeinfo
assert quick_and_dirty, "Should never get here in normal mode"
AssertionError: Should never get here in normal mode

# Fixed code
def add_symbol(self, name: str, node: SymbolTableNode, context: Context) -> None:
    # NOTE: This is closely related to SemanticAnalyzerPass2.add_symbol. Since both methods
    #     will be called on top-level definitions, they need to co-operate. If you change
    #     this, you may have to change the other method as well.
    if self.sem.is_func_scope():
        assert self.sem.locals[-1] is not None
        if name in self.sem.locals[-1]:
            # Flag redefinition unless this is a reimport of a module.
            if not (
                node.kind == MODULE_REF and self.sem.locals[-1][name].node == node.node
            ):
                self.sem.name_already_defined(name, context, self.sem.locals[-1][name])
                return
        self.sem.locals[-1][name] = node
    else:
        assert self.sem.type is None  # Pass 1 doesn't look inside classes
        existing = self.sem.globals.get(name)
        if (
            existing
            and (not isinstance(node.node, MypyFile) or existing.node != node.node)
            and existing.kind != UNBOUND_IMPORTED
            and not isinstance(existing.node, ImportedName)
        ):
            # Modules can be imported multiple times to support import
            # of multiple submodules of a package (e.g. a.x and a.y).
            ok = False
            # Only report an error if the symbol collision provides a different type.
            if existing.type and node.type and is_same_type(existing.type, node.type):
                ok = True
            if not ok:
                self.sem.name_already_defined(name, context, existing)
                return
        elif not existing:
            self.sem.globals[name] = node