# Buggy code
def infer_function_type_arguments_pass2(
    self,
    callee_type: CallableType,
    args: List[Node],
    arg_kinds: List[int],
    formal_to_actual: List[List[int]],
    inferred_args: List[Type],
    context: Context,
) -> Tuple[CallableType, List[Type]]:
    """Perform second pass of generic function type argument inference.

    The second pass is needed for arguments with types such as Callable[[T], S],
    where both T and S are type variables, when the actual argument is a
    lambda with inferred types.  The idea is to infer the type variable T
    in the first pass (based on the types of other arguments).  This lets
    us infer the argument and return type of the lambda expression and
    thus also the type variable S in this second pass.

    Return (the callee with type vars applied, inferred actual arg types).
    """
    # None or erased types in inferred types mean that there was not enough
    # information to infer the argument. Replace them with None values so
    # that they are not applied yet below.
    for i, arg in enumerate(inferred_args):
        if isinstance(arg, NoneTyp) or isinstance(arg, ErasedType):
            inferred_args[i] = None

    callee_type = cast(
        CallableType, self.apply_generic_arguments(callee_type, inferred_args, context)
    )
    arg_types = self.infer_arg_types_in_context2(
        callee_type, args, arg_kinds, formal_to_actual
    )

    inferred_args = infer_function_type_arguments(
        callee_type, arg_types, arg_kinds, formal_to_actual
    )

    return callee_type, inferred_args


# 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 infer_function_type_arguments_pass2(
    self,
    callee_type: CallableType,
    args: List[Node],
    arg_kinds: List[int],
    formal_to_actual: List[List[int]],
    inferred_args: List[Type],
    context: Context,
) -> Tuple[CallableType, List[Type]]:
    """Perform second pass of generic function type argument inference.

    The second pass is needed for arguments with types such as Callable[[T], S],
    where both T and S are type variables, when the actual argument is a
    lambda with inferred types.  The idea is to infer the type variable T
    in the first pass (based on the types of other arguments).  This lets
    us infer the argument and return type of the lambda expression and
    thus also the type variable S in this second pass.

    Return (the callee with type vars applied, inferred actual arg types).
    """
    # None or erased types in inferred types mean that there was not enough
    # information to infer the argument. Replace them with None values so
    # that they are not applied yet below.
    for i, arg in enumerate(inferred_args):
        if isinstance(arg, NoneTyp) or has_erased_component(arg):
            inferred_args[i] = None

    callee_type = cast(
        CallableType, self.apply_generic_arguments(callee_type, inferred_args, context)
    )
    arg_types = self.infer_arg_types_in_context2(
        callee_type, args, arg_kinds, formal_to_actual
    )

    inferred_args = infer_function_type_arguments(
        callee_type, arg_types, arg_kinds, formal_to_actual
    )

    return callee_type, inferred_args