# Buggy code
def try_infer_partial_type_from_indexed_assignment(
    self, lvalue: IndexExpr, rvalue: Node
) -> None:
    # TODO: Should we share some of this with try_infer_partial_type?
    partial_types = self.partial_types[-1]
    if not partial_types:
        # Fast path leave -- no partial types in the current scope.
        return
    if isinstance(lvalue.base, RefExpr):
        var = lvalue.base.node
        if var in partial_types:
            var = cast(Var, var)
            typename = cast(Instance, var.type).type.fullname()
            if typename == "builtins.dict":
                # TODO: Don't infer things twice.
                key_type = self.accept(lvalue.index)
                value_type = self.accept(rvalue)
                if is_valid_inferred_type(key_type) and is_valid_inferred_type(
                    value_type
                ):
                    var.type = self.named_generic_type(
                        "builtins.dict", [key_type, value_type]
                    )
                    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_from_indexed_assignment(
    self, lvalue: IndexExpr, rvalue: Node
) -> None:
    # TODO: Should we share some of this with try_infer_partial_type?
    if isinstance(lvalue.base, RefExpr):
        var = cast(Var, lvalue.base.node)
        partial_types = self.find_partial_types(var)
        if partial_types is not None:
            typename = cast(Instance, var.type).type.fullname()
            if typename == "builtins.dict":
                # TODO: Don't infer things twice.
                key_type = self.accept(lvalue.index)
                value_type = self.accept(rvalue)
                if is_valid_inferred_type(key_type) and is_valid_inferred_type(
                    value_type
                ):
                    var.type = self.named_generic_type(
                        "builtins.dict", [key_type, value_type]
                    )
                    del partial_types[var]