# Buggy code
def accept(
    self,
    node: Expression,
    type_context: Optional[Type] = None,
    allow_none_return: bool = False,
    always_allow_any: bool = False,
) -> Type:
    """Type check a node in the given type context.  If allow_none_return
    is True and this expression is a call, allow it to return None.  This
    applies only to this expression and not any subexpressions.
    """
    if node in self.type_overrides:
        return self.type_overrides[node]
    self.type_context.append(type_context)
    try:
        if allow_none_return and isinstance(node, CallExpr):
            typ = self.visit_call_expr(node, allow_none_return=True)
        elif allow_none_return and isinstance(node, YieldFromExpr):
            typ = self.visit_yield_from_expr(node, allow_none_return=True)
        elif allow_none_return and isinstance(node, ConditionalExpr):
            typ = self.visit_conditional_expr(node, allow_none_return=True)
        else:
            typ = node.accept(self)
    except Exception as err:
        report_internal_error(
            err, self.chk.errors.file, node.line, self.chk.errors, self.chk.options
        )

    self.type_context.pop()
    assert typ is not None
    self.chk.store_type(node, typ)

    if (
        self.chk.options.disallow_any_expr
        and not always_allow_any
        and not self.chk.is_stub
        and self.chk.in_checked_function()
        and has_any_type(typ)
        and not self.chk.current_node_deferred
    ):
        self.msg.disallowed_any_type(typ, node)

    if not self.chk.in_checked_function() or self.chk.current_node_deferred:
        return AnyType(TypeOfAny.unannotated)
    else:
        return typ


# Traceback (KeyError)
test.py:3: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.790+dev.6ee562a8f3e69ac134f8c501b79b3712c9e24bbd
Traceback (most recent call last):
File "***/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "***/python3.8/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "***/python3.8/site-packages/mypy/main.py", line 89, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "***/python3.8/site-packages/mypy/build.py", line 180, in build
result = _build(
File "***/python3.8/site-packages/mypy/build.py", line 252, in _build
graph = dispatch(sources, manager, stdout)
File "***/python3.8/site-packages/mypy/build.py", line 2626, in dispatch
process_graph(graph, manager)
File "***/python3.8/site-packages/mypy/build.py", line 2949, in process_graph
process_stale_scc(graph, scc, manager)
File "***/python3.8/site-packages/mypy/build.py", line 3047, in process_stale_scc
graph[id].type_check_first_pass()
File "***/python3.8/site-packages/mypy/build.py", line 2107, in type_check_first_pass
self.type_checker().check_first_pass()
File "***/python3.8/site-packages/mypy/checker.py", line 294, in check_first_pass
self.accept(d)
File "***/python3.8/site-packages/mypy/checker.py", line 401, in accept
stmt.accept(self)
File "***/python3.8/site-packages/mypy/nodes.py", line 677, in accept
return visitor.visit_func_def(self)
File "***/python3.8/site-packages/mypy/checker.py", line 726, in visit_func_def
self._visit_func_def(defn)
File "***/python3.8/site-packages/mypy/checker.py", line 730, in _visit_func_def
self.check_func_item(defn, name=defn.name)
File "***/python3.8/site-packages/mypy/checker.py", line 792, in check_func_item
self.check_func_def(defn, typ, name)
File "***/python3.8/site-packages/mypy/checker.py", line 975, in check_func_def
self.accept(item.body)
File "***/python3.8/site-packages/mypy/checker.py", line 401, in accept
stmt.accept(self)
File "***/python3.8/site-packages/mypy/nodes.py", line 1005, in accept
return visitor.visit_block(self)
File "***/python3.8/site-packages/mypy/checker.py", line 1973, in visit_block
self.accept(s)
File "***/python3.8/site-packages/mypy/checker.py", line 401, in accept
stmt.accept(self)
File "***/python3.8/site-packages/mypy/nodes.py", line 1196, in accept
return visitor.visit_if_stmt(self)
File "***/python3.8/site-packages/mypy/checker.py", line 3212, in visit_if_stmt
if_map, else_map = self.find_isinstance_check(e)
File "***/python3.8/site-packages/mypy/checker.py", line 3928, in find_isinstance_check
if_map, else_map = self.find_isinstance_check_helper(node)
File "***/python3.8/site-packages/mypy/checker.py", line 4104, in find_isinstance_check_helper
return self.find_isinstance_check_helper(node.target)
File "***/python3.8/site-packages/mypy/checker.py", line 4108, in find_isinstance_check_helper
vartype = type_map[node]
KeyError: <mypy.nodes.NameExpr object at 0x7fb2f0768940>
test.py:3: : note: use --pdb to drop into pdb

# Fixed code
def accept(
    self,
    node: Expression,
    type_context: Optional[Type] = None,
    allow_none_return: bool = False,
    always_allow_any: bool = False,
) -> Type:
    """Type check a node in the given type context.  If allow_none_return
    is True and this expression is a call, allow it to return None.  This
    applies only to this expression and not any subexpressions.
    """
    if node in self.type_overrides:
        return self.type_overrides[node]
    self.type_context.append(type_context)
    try:
        if allow_none_return and isinstance(node, CallExpr):
            typ = self.visit_call_expr(node, allow_none_return=True)
        elif allow_none_return and isinstance(node, YieldFromExpr):
            typ = self.visit_yield_from_expr(node, allow_none_return=True)
        elif allow_none_return and isinstance(node, ConditionalExpr):
            typ = self.visit_conditional_expr(node, allow_none_return=True)
        else:
            typ = node.accept(self)
    except Exception as err:
        report_internal_error(
            err, self.chk.errors.file, node.line, self.chk.errors, self.chk.options
        )

    self.type_context.pop()
    assert typ is not None
    self.chk.store_type(node, typ)

    if isinstance(node, AssignmentExpr) and not has_uninhabited_component(typ):
        self.chk.store_type(node.target, typ)

    if (
        self.chk.options.disallow_any_expr
        and not always_allow_any
        and not self.chk.is_stub
        and self.chk.in_checked_function()
        and has_any_type(typ)
        and not self.chk.current_node_deferred
    ):
        self.msg.disallowed_any_type(typ, node)

    if not self.chk.in_checked_function() or self.chk.current_node_deferred:
        return AnyType(TypeOfAny.unannotated)
    else:
        return typ