# Buggy code
def visit_func_def(self, defn: FuncDef) -> Type:
    """Type check a function definition."""
    self.check_func_item(defn, name=defn.name())
    if defn.info:
        if not defn.is_dynamic():
            self.check_method_override(defn)
        self.check_inplace_operator_method(defn)
    if defn.original_def:
        # Override previous definition.
        new_type = self.function_type(defn)
        if isinstance(defn.original_def, FuncDef):
            # Function definition overrides function definition.
            if not is_same_type(new_type, self.function_type(defn.original_def)):
                self.msg.incompatible_conditional_function_def(defn)
        else:
            # Function definition overrides a variable initialized via assignment.
            orig_type = defn.original_def.type
            if isinstance(orig_type, PartialType):
                if orig_type.type is None:
                    # Ah this is a partial type. Give it the type of the function.
                    defn.original_def.type = new_type
                    partial_types = self.partial_types[-1]
                    del partial_types[defn.original_def]
                else:
                    # Trying to redefine something like partial empty list as function.
                    self.fail(messages.INCOMPATIBLE_REDEFINITION, defn)
            else:
                # TODO: Update conditional type binder.
                self.check_subtype(
                    orig_type,
                    new_type,
                    defn,
                    messages.INCOMPATIBLE_REDEFINITION,
                    "original type",
                    "redefinition with type",
                )


# 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 visit_func_def(self, defn: FuncDef) -> Type:
    """Type check a function definition."""
    self.check_func_item(defn, name=defn.name())
    if defn.info:
        if not defn.is_dynamic():
            self.check_method_override(defn)
        self.check_inplace_operator_method(defn)
    if defn.original_def:
        # Override previous definition.
        new_type = self.function_type(defn)
        if isinstance(defn.original_def, FuncDef):
            # Function definition overrides function definition.
            if not is_same_type(new_type, self.function_type(defn.original_def)):
                self.msg.incompatible_conditional_function_def(defn)
        else:
            # Function definition overrides a variable initialized via assignment.
            orig_type = defn.original_def.type
            if isinstance(orig_type, PartialType):
                if orig_type.type is None:
                    # Ah this is a partial type. Give it the type of the function.
                    var = defn.original_def
                    partial_types = self.find_partial_types(var)
                    if partial_types is not None:
                        var.type = new_type
                        del partial_types[var]
                else:
                    # Trying to redefine something like partial empty list as function.
                    self.fail(messages.INCOMPATIBLE_REDEFINITION, defn)
            else:
                # TODO: Update conditional type binder.
                self.check_subtype(
                    orig_type,
                    new_type,
                    defn,
                    messages.INCOMPATIBLE_REDEFINITION,
                    "original type",
                    "redefinition with type",
                )