# Buggy code
def apply_inferred_arguments(
    self, callee_type: CallableType, inferred_args: List[Type], context: Context
) -> CallableType:
    """Apply inferred values of type arguments to a generic function.

    Inferred_args contains the values of function type arguments.
    """
    # Report error if some of the variables could not be solved. In that
    # case assume that all variables have type Any to avoid extra
    # bogus error messages.
    for i, inferred_type in enumerate(inferred_args):
        if not inferred_type:
            # Could not infer a non-trivial type for a type variable.
            self.msg.could_not_infer_type_arguments(callee_type, i + 1, context)
            inferred_args = [AnyType()] * len(inferred_args)
    # Apply the inferred types to the function type. In this case the
    # return type must be CallableType, since we give the right number of type
    # arguments.
    return cast(
        CallableType, self.apply_generic_arguments(callee_type, inferred_args, context)
    )


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/bin/.mypy-wrapped", line 7, in <module>
main(__file__)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/main.py", line 54, in main
res = type_check_only(sources, bin_dir, options)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/main.py", line 102, in type_check_only
python_path=options.python_path)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/build.py", line 209, in build
dispatch(sources, manager)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/build.py", line 1255, in dispatch
process_graph(graph, manager)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/build.py", line 1386, in process_graph
process_stale_scc(graph, scc)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/build.py", line 1416, in process_stale_scc
graph[id].type_check()
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/build.py", line 1235, in type_check
manager.type_checker.visit_file(self.tree, self.xpath)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 419, in visit_file
self.accept(d)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 460, in accept
typ = node.accept(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/nodes.py", line 462, in accept
return visitor.visit_func_def(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 573, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 631, in check_func_item
self.check_func_def(defn, typ, name)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 739, in check_func_def
self.accept_in_frame(item.body)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 475, in accept_in_frame
answer = self.accept(node, type_context)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 460, in accept
typ = node.accept(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/nodes.py", line 715, in accept
return visitor.visit_block(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 1132, in visit_block
self.accept(s)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 460, in accept
typ = node.accept(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/nodes.py", line 753, in accept
return visitor.visit_assignment_stmt(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 1141, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 1192, in check_assignment
rvalue)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 1424, in infer_variable_type
elif not is_valid_inferred_type(init_type):
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 2549, in is_valid_inferred_type
if not is_valid_inferred_type(item):
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/checker.py", line 2541, in is_valid_inferred_type
if is_same_type(typ, NoneTyp()):
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/sametypes.py", line 27, in is_same_type
return left.accept(SameTypeVisitor(right))
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/types.py", line 244, in accept
return visitor.visit_erased_type(self)
File "/nix/store/czly6jrafckfvwm9mjc6wvandc2ddwi9-python3.5-mypy-lang-0.4.1/lib/python3.5/site-packages/mypy/sametypes.py", line 74, in visit_erased_type
raise RuntimeError()
RuntimeError:

*** INTERNAL ERROR ***

# Fixed code
def apply_inferred_arguments(
    self, callee_type: CallableType, inferred_args: List[Type], context: Context
) -> CallableType:
    """Apply inferred values of type arguments to a generic function.

    Inferred_args contains the values of function type arguments.
    """
    # Report error if some of the variables could not be solved. In that
    # case assume that all variables have type Any to avoid extra
    # bogus error messages.
    for i, inferred_type in enumerate(inferred_args):
        if not inferred_type or has_erased_component(inferred_type):
            # Could not infer a non-trivial type for a type variable.
            self.msg.could_not_infer_type_arguments(callee_type, i + 1, context)
            inferred_args = [AnyType()] * len(inferred_args)
    # Apply the inferred types to the function type. In this case the
    # return type must be CallableType, since we give the right number of type
    # arguments.
    return cast(
        CallableType, self.apply_generic_arguments(callee_type, inferred_args, context)
    )