# Buggy code
def visit_file(
    self,
    file_node: MypyFile,
    fnam: str,
    options: Options,
    patches: List[Callable[[], None]],
) -> None:
    """Run semantic analysis phase 2 over a file.

    Add callbacks by mutating the patches list argument. They will be called
    after all semantic analysis phases but before type checking.
    """
    self.recurse_into_functions = True
    self.options = options
    self.errors.set_file(fnam, file_node.fullname())
    self.cur_mod_node = file_node
    self.cur_mod_id = file_node.fullname()
    self.is_stub_file = fnam.lower().endswith(".pyi")
    self.is_typeshed_stub_file = self.errors.is_typeshed_file(file_node.path)
    self.globals = file_node.names
    self.patches = patches

    with experiments.strict_optional_set(options.strict_optional):
        if "builtins" in self.modules:
            self.globals["__builtins__"] = SymbolTableNode(
                MODULE_REF, self.modules["builtins"]
            )

        for name in implicit_module_attrs:
            v = self.globals[name].node
            if isinstance(v, Var):
                assert v.type is not None, "Type of implicit attribute not set"
                v.type = self.anal_type(v.type)
                v.is_ready = True

        defs = file_node.defs
        for d in defs:
            self.accept(d)

        if self.cur_mod_id == "builtins":
            remove_imported_names_from_symtable(self.globals, "builtins")
            for alias_name in type_aliases:
                self.globals.pop(alias_name.split(".")[-1], None)

        if "__all__" in self.globals:
            for name, g in self.globals.items():
                if name not in self.all_exports:
                    g.module_public = False

        del self.options
        del self.patches


# Traceback (RuntimeError)
forward.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.540
Traceback (most recent call last):
File "/Users/joshstaiger/anaconda/bin/mypy", line 11, in <module>
sys.exit(console_entry())
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/main.py", line 50, in main
res = type_check_only(sources, bin_dir, options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/main.py", line 103, in type_check_only
options=options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 201, in build
graph = dispatch(sources, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1867, in dispatch
process_graph(graph, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2117, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2214, in process_stale_scc
graph[id].semantic_analysis_pass_three()
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1760, in semantic_analysis_pass_three
self.options, patches)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 56, in visit_file
self.accept(file_node)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 73, in accept
node.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 228, in accept
return visitor.visit_mypy_file(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/traverser.py", line 30, in visit_mypy_file
d.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 794, in accept
return visitor.visit_assignment_stmt(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 187, in visit_assignment_stmt
self.analyze(s.rvalue.analyzed.type, s.rvalue.analyzed, warn=True)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/semanal_pass3.py", line 311, in analyze
type.accept(analyzer)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 476, in accept
return visitor.visit_instance(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/typeanal.py", line 716, in visit_instance
if not is_subtype(arg, bound):
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 83, in is_subtype
ignore_declared_variance=ignore_declared_variance))
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1227, in accept
return visitor.visit_union_type(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 312, in visit_union_type
for item in left.items)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 312, in <genexpr>
for item in left.items)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/subtypes.py", line 83, in is_subtype
ignore_declared_variance=ignore_declared_variance))
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1412, in accept
return visitor.visit_forwardref_type(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/types.py", line 1497, in visit_forwardref_type
raise RuntimeError('Internal error: unresolved forward reference')
RuntimeError: Internal error: unresolved forward reference
forward.py:1: : note: use --pdb to drop into pdb

# Fixed code
def visit_file(
    self,
    file_node: MypyFile,
    fnam: str,
    options: Options,
    patches: List[Tuple[int, Callable[[], None]]],
) -> None:
    """Run semantic analysis phase 2 over a file.

    Add (priority, callback) pairs by mutating the 'patches' list argument. They
    will be called after all semantic analysis phases but before type checking,
    lowest priority values first.
    """
    self.recurse_into_functions = True
    self.options = options
    self.errors.set_file(fnam, file_node.fullname())
    self.cur_mod_node = file_node
    self.cur_mod_id = file_node.fullname()
    self.is_stub_file = fnam.lower().endswith(".pyi")
    self.is_typeshed_stub_file = self.errors.is_typeshed_file(file_node.path)
    self.globals = file_node.names
    self.patches = patches

    with experiments.strict_optional_set(options.strict_optional):
        if "builtins" in self.modules:
            self.globals["__builtins__"] = SymbolTableNode(
                MODULE_REF, self.modules["builtins"]
            )

        for name in implicit_module_attrs:
            v = self.globals[name].node
            if isinstance(v, Var):
                assert v.type is not None, "Type of implicit attribute not set"
                v.type = self.anal_type(v.type)
                v.is_ready = True

        defs = file_node.defs
        for d in defs:
            self.accept(d)

        if self.cur_mod_id == "builtins":
            remove_imported_names_from_symtable(self.globals, "builtins")
            for alias_name in type_aliases:
                self.globals.pop(alias_name.split(".")[-1], None)

        if "__all__" in self.globals:
            for name, g in self.globals.items():
                if name not in self.all_exports:
                    g.module_public = False

        del self.options
        del self.patches