# Buggy code
def try_infer_partial_type(self, e: CallExpr) -> None:
    partial_types = self.chk.partial_types[-1]
    if not partial_types:
        # Fast path leave -- no partial types in the current scope.
        return
    if isinstance(e.callee, MemberExpr) and isinstance(e.callee.expr, RefExpr):
        var = e.callee.expr.node
        if var in partial_types:
            var = cast(Var, var)
            partial_type_type = cast(PartialType, var.type).type
            if partial_type_type is None:
                # A partial None type -> can't infer anything.
                return
            typename = partial_type_type.fullname()
            methodname = e.callee.name
            # Sometimes we can infer a full type for a partial List, Dict or Set type.
            # TODO: Don't infer argument expression twice.
            if (
                typename in self.item_args
                and methodname in self.item_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                item_type = self.accept(e.args[0])
                if mypy.checker.is_valid_inferred_type(item_type):
                    var.type = self.chk.named_generic_type(typename, [item_type])
                    del partial_types[var]
            elif (
                typename in self.container_args
                and methodname in self.container_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                arg_type = self.accept(e.args[0])
                if isinstance(arg_type, Instance):
                    arg_typename = arg_type.type.fullname()
                    if arg_typename in self.container_args[typename][methodname]:
                        if all(
                            mypy.checker.is_valid_inferred_type(item_type)
                            for item_type in arg_type.args
                        ):
                            var.type = self.chk.named_generic_type(
                                typename, list(arg_type.args)
                            )
                            del partial_types[var]


# 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 try_infer_partial_type(self, e: CallExpr) -> None:
    if isinstance(e.callee, MemberExpr) and isinstance(e.callee.expr, RefExpr):
        var = cast(Var, e.callee.expr.node)
        partial_types = self.chk.find_partial_types(var)
        if partial_types is not None:
            partial_type_type = cast(PartialType, var.type).type
            if partial_type_type is None:
                # A partial None type -> can't infer anything.
                return
            typename = partial_type_type.fullname()
            methodname = e.callee.name
            # Sometimes we can infer a full type for a partial List, Dict or Set type.
            # TODO: Don't infer argument expression twice.
            if (
                typename in self.item_args
                and methodname in self.item_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                item_type = self.accept(e.args[0])
                if mypy.checker.is_valid_inferred_type(item_type):
                    var.type = self.chk.named_generic_type(typename, [item_type])
                    del partial_types[var]
            elif (
                typename in self.container_args
                and methodname in self.container_args[typename]
                and e.arg_kinds == [ARG_POS]
            ):
                arg_type = self.accept(e.args[0])
                if isinstance(arg_type, Instance):
                    arg_typename = arg_type.type.fullname()
                    if arg_typename in self.container_args[typename][methodname]:
                        if all(
                            mypy.checker.is_valid_inferred_type(item_type)
                            for item_type in arg_type.args
                        ):
                            var.type = self.chk.named_generic_type(
                                typename, list(arg_type.args)
                            )
                            del partial_types[var]