# Buggy code
def module_not_found(self, path: str, id: str, line: int, target: str) -> None:
    self.errors.set_file(path, id)
    stub_msg = "(Stub files are from https://github.com/python/typeshed)"
    if (
        self.options.python_version[0] == 2 and moduleinfo.is_py2_std_lib_module(target)
    ) or (
        self.options.python_version[0] >= 3 and moduleinfo.is_py3_std_lib_module(target)
    ):
        self.errors.report(
            line,
            0,
            "No library stub file for standard library module '{}'".format(target),
        )
        self.errors.report(line, 0, stub_msg, severity="note", only_once=True)
    elif moduleinfo.is_third_party_module(target):
        self.errors.report(
            line, 0, "No library stub file for module '{}'".format(target)
        )
        self.errors.report(line, 0, stub_msg, severity="note", only_once=True)
    else:
        self.errors.report(line, 0, "Cannot find module named '{}'".format(target))
        self.errors.report(
            line,
            0,
            "(Perhaps setting MYPYPATH "
            'or using the "--ignore-missing-imports" flag would help)',
            severity="note",
            only_once=True,
        )


# Traceback (KeyError)
file.py: error: Name '__builtins__' is not defined
file.py:1: error: Cannot find module named 'builtins'
file.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
file.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.550
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 66, in main
res = type_check_only(sources, bin_dir, options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/main.py", line 119, in type_check_only
options=options)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 214, in build
graph = dispatch(sources, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1935, in dispatch
process_graph(graph, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2199, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 2327, in process_stale_scc
graph[id].type_check_first_pass()
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/build.py", line 1836, in type_check_first_pass
self.type_checker.check_first_pass()
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checker.py", line 196, in check_first_pass
self.accept(d)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checker.py", line 283, in accept
stmt.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 764, in accept
return visitor.visit_expression_stmt(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checker.py", line 2072, in visit_expression_stmt
self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checkexpr.py", line 2363, in accept
typ = node.accept(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/nodes.py", line 1024, in accept
return visitor.visit_int_expr(self)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checkexpr.py", line 1320, in visit_int_expr
return self.named_type('builtins.int')
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checkexpr.py", line 2387, in named_type
return self.chk.named_type(name)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checker.py", line 2636, in named_type
sym = self.lookup_qualified(name)
File "/Users/joshstaiger/anaconda/lib/python3.6/site-packages/mypy/checker.py", line 2702, in lookup_qualified
n = self.modules[parts[0]]
KeyError: 'builtins'
file.py:1: : note: use --pdb to drop into pdb

# Fixed code
def module_not_found(self, path: str, source: str, line: int, target: str) -> None:
    self.errors.set_file(path, source)
    stub_msg = "(Stub files are from https://github.com/python/typeshed)"
    if target == "builtins":
        self.errors.report(
            line,
            0,
            "Cannot find 'builtins' module. Typeshed appears broken!",
            blocker=True,
        )
        self.errors.raise_error()
    elif (
        self.options.python_version[0] == 2 and moduleinfo.is_py2_std_lib_module(target)
    ) or (
        self.options.python_version[0] >= 3 and moduleinfo.is_py3_std_lib_module(target)
    ):
        self.errors.report(
            line,
            0,
            "No library stub file for standard library module '{}'".format(target),
        )
        self.errors.report(line, 0, stub_msg, severity="note", only_once=True)
    elif moduleinfo.is_third_party_module(target):
        self.errors.report(
            line, 0, "No library stub file for module '{}'".format(target)
        )
        self.errors.report(line, 0, stub_msg, severity="note", only_once=True)
    else:
        self.errors.report(line, 0, "Cannot find module named '{}'".format(target))
        self.errors.report(
            line,
            0,
            "(Perhaps setting MYPYPATH "
            'or using the "--ignore-missing-imports" flag would help)',
            severity="note",
            only_once=True,
        )