# Buggy code
def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
    result = None  # type: Type
    node = e.node
    if isinstance(node, Var):
        # Variable reference.
        result = self.analyze_var_ref(node, e)
        if isinstance(result, PartialType):
            if result.type is None:
                # 'None' partial type. It has a well-defined type. In an lvalue context
                # we want to preserve the knowledge of it being a partial type.
                if not lvalue:
                    result = NoneTyp()
            else:
                partial_types = self.chk.partial_types[-1]
                if node in partial_types:
                    context = partial_types[node]
                    self.msg.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
                result = AnyType()
    elif isinstance(node, FuncDef):
        # Reference to a global function.
        result = function_type(node, self.named_type("builtins.function"))
    elif isinstance(node, OverloadedFuncDef):
        result = node.type
    elif isinstance(node, TypeInfo):
        # Reference to a type object.
        result = type_object_type(node, self.named_type)
    elif isinstance(node, MypyFile):
        # Reference to a module object.
        result = self.named_type("builtins.module")
    elif isinstance(node, Decorator):
        result = self.analyze_var_ref(node.var, e)
    else:
        # Unknown reference; use any type implicitly to avoid
        # generating extra type errors.
        result = AnyType()
    return result


# Traceback (KeyError)
Traceback (most recent call last):
File "./scripts/mypy", line 6, in <module>
main(__file__)
File "/home/dshea/src/mypy/mypy/main.py", line 53, in main
type_check_only(sources, bin_dir, options)
File "/home/dshea/src/mypy/mypy/main.py", line 96, in type_check_only
python_path=options.python_path)
File "/home/dshea/src/mypy/mypy/build.py", line 203, in build
result = manager.process(initial_states)
File "/home/dshea/src/mypy/mypy/build.py", line 386, in process
next.process()
File "/home/dshea/src/mypy/mypy/build.py", line 883, in process
self.type_checker().visit_file(self.tree, self.tree.path)
File "/home/dshea/src/mypy/mypy/checker.py", line 385, in visit_file
self.accept(d)
File "/home/dshea/src/mypy/mypy/checker.py", line 394, in accept
typ = node.accept(self)
File "/home/dshea/src/mypy/mypy/nodes.py", line 383, in accept
return visitor.visit_func_def(self)
File "/home/dshea/src/mypy/mypy/checker.py", line 449, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/home/dshea/src/mypy/mypy/checker.py", line 505, in check_func_item
self.check_func_def(defn, typ, name)
File "/home/dshea/src/mypy/mypy/checker.py", line 588, in check_func_def
self.accept_in_frame(item.body)
File "/home/dshea/src/mypy/mypy/checker.py", line 409, in accept_in_frame
answer = self.accept(node, type_context)
File "/home/dshea/src/mypy/mypy/checker.py", line 394, in accept
typ = node.accept(self)
File "/home/dshea/src/mypy/mypy/nodes.py", line 527, in accept
return visitor.visit_block(self)
File "/home/dshea/src/mypy/mypy/checker.py", line 983, in visit_block
self.accept(s)
File "/home/dshea/src/mypy/mypy/checker.py", line 394, in accept
typ = node.accept(self)
File "/home/dshea/src/mypy/mypy/nodes.py", line 565, in accept
return visitor.visit_assignment_stmt(self)
File "/home/dshea/src/mypy/mypy/checker.py", line 992, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
File "/home/dshea/src/mypy/mypy/checker.py", line 1021, in check_assignment
del partial_types[lvalue_type.var]
KeyError: <mypy.nodes.Var object at 0x7ffbeee8f6d8>

*** INTERNAL ERROR ***

/usr/lib64/python3.5/mimetypes.py:359: error: Internal error -- please report a bug at https://github.com/JukkaL/mypy/issues

NOTE: you can use "mypy --pdb ..." to drop into the debugger when this happens.

# Fixed code
def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
    result = None  # type: Type
    node = e.node
    if isinstance(node, Var):
        # Variable reference.
        result = self.analyze_var_ref(node, e)
        if isinstance(result, PartialType):
            if result.type is None:
                # 'None' partial type. It has a well-defined type. In an lvalue context
                # we want to preserve the knowledge of it being a partial type.
                if not lvalue:
                    result = NoneTyp()
            else:
                partial_types = self.chk.find_partial_types(node)
                if partial_types is not None:
                    context = partial_types[node]
                    self.msg.fail(messages.NEED_ANNOTATION_FOR_VAR, context)
                result = AnyType()
    elif isinstance(node, FuncDef):
        # Reference to a global function.
        result = function_type(node, self.named_type("builtins.function"))
    elif isinstance(node, OverloadedFuncDef):
        result = node.type
    elif isinstance(node, TypeInfo):
        # Reference to a type object.
        result = type_object_type(node, self.named_type)
    elif isinstance(node, MypyFile):
        # Reference to a module object.
        result = self.named_type("builtins.module")
    elif isinstance(node, Decorator):
        result = self.analyze_var_ref(node.var, e)
    else:
        # Unknown reference; use any type implicitly to avoid
        # generating extra type errors.
        result = AnyType()
    return result