# Buggy code
def check_assignment(
    self, lvalue: Node, rvalue: Node, infer_lvalue_type: bool = True
) -> None:
    """Type check a single assignment: lvalue = rvalue."""
    if isinstance(lvalue, TupleExpr) or isinstance(lvalue, ListExpr):
        ltuple = cast(Union[TupleExpr, ListExpr], lvalue)

        self.check_assignment_to_multiple_lvalues(
            ltuple.items, rvalue, lvalue, infer_lvalue_type
        )
    else:
        lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue)
        if lvalue_type:
            if isinstance(lvalue_type, PartialType) and lvalue_type.type is None:
                # Try to infer a proper type for a variable with a partial None type.
                rvalue_type = self.accept(rvalue)
                if isinstance(rvalue_type, NoneTyp):
                    # This doesn't actually provide any additional information -- multiple
                    # None initializers preserve the partial None type.
                    return
                if is_valid_inferred_type(rvalue_type):
                    lvalue_type.var.type = rvalue_type
                    partial_types = self.partial_types[-1]
                    del partial_types[lvalue_type.var]
                # Try to infer a partial type. No need to check the return value, as
                # an error will be reported elsewhere.
                self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type)
                return
            rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue)

            if rvalue_type and infer_lvalue_type:
                self.binder.assign_type(lvalue, rvalue_type, self.typing_mode_weak())
        elif index_lvalue:
            self.check_indexed_assignment(index_lvalue, rvalue, rvalue)

        if inferred:
            self.infer_variable_type(inferred, lvalue, self.accept(rvalue), rvalue)


# 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 check_assignment(
    self, lvalue: Node, rvalue: Node, infer_lvalue_type: bool = True
) -> None:
    """Type check a single assignment: lvalue = rvalue."""
    if isinstance(lvalue, TupleExpr) or isinstance(lvalue, ListExpr):
        ltuple = cast(Union[TupleExpr, ListExpr], lvalue)

        self.check_assignment_to_multiple_lvalues(
            ltuple.items, rvalue, lvalue, infer_lvalue_type
        )
    else:
        lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue)
        if lvalue_type:
            if isinstance(lvalue_type, PartialType) and lvalue_type.type is None:
                # Try to infer a proper type for a variable with a partial None type.
                rvalue_type = self.accept(rvalue)
                if isinstance(rvalue_type, NoneTyp):
                    # This doesn't actually provide any additional information -- multiple
                    # None initializers preserve the partial None type.
                    return
                if is_valid_inferred_type(rvalue_type):
                    var = lvalue_type.var
                    partial_types = self.find_partial_types(var)
                    if partial_types is not None:
                        var.type = rvalue_type
                        del partial_types[var]
                # Try to infer a partial type. No need to check the return value, as
                # an error will be reported elsewhere.
                self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type)
                return
            rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue)

            if rvalue_type and infer_lvalue_type:
                self.binder.assign_type(lvalue, rvalue_type, self.typing_mode_weak())
        elif index_lvalue:
            self.check_indexed_assignment(index_lvalue, rvalue, rvalue)

        if inferred:
            self.infer_variable_type(inferred, lvalue, self.accept(rvalue), rvalue)