# Buggy code
def infer_function_type_arguments_using_context(
    self, callable: CallableType, error_context: Context
) -> CallableType:
    """Unify callable return type to type context to infer type vars.

    For example, if the return type is set[t] where 't' is a type variable
    of callable, and if the context is set[int], return callable modified
    by substituting 't' with 'int'.
    """
    ctx = self.type_context[-1]
    if not ctx:
        return callable
    # The return type may have references to type metavariables that
    # we are inferring right now. We must consider them as indeterminate
    # and they are not potential results; thus we replace them with the
    # special ErasedType type. On the other hand, class type variables are
    # valid results.
    erased_ctx = replace_meta_vars(ctx, ErasedType())
    ret_type = callable.ret_type
    if isinstance(ret_type, TypeVarType):
        if ret_type.values or (not isinstance(ctx, Instance) or not ctx.args):
            # The return type is a type variable. If it has values, we can't easily restrict
            # type inference to conform to the valid values. If it's unrestricted, we could
            # infer a too general type for the type variable if we use context, and this could
            # result in confusing and spurious type errors elsewhere.
            #
            # Give up and just use function arguments for type inference. As an exception,
            # if the context is a generic instance type, actually use it as context, as
            # this *seems* to usually be the reasonable thing to do.
            #
            # See also github issues #462 and #360.
            ret_type = NoneTyp()
    args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
    # Only substitute non-Uninhabited and non-erased types.
    new_args = []  # type: List[Optional[Type]]
    for arg in args:
        if isinstance(arg, UninhabitedType) or has_erased_component(arg):
            new_args.append(None)
        else:
            new_args.append(arg)
    return self.apply_generic_arguments(callable, new_args, error_context)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/Users/ambv/.venvs/mypy/bin/mypy", line 11, in <module>
load_entry_point('mypy', 'console_scripts', 'mypy')()
File "mypy/__main__.py", line 7, in console_entry
main(None)
File "mypy/main.py", line 50, in main
res = type_check_only(sources, bin_dir, options)
File "mypy/main.py", line 103, in type_check_only
options=options)
File "mypy/build.py", line 198, in build
graph = dispatch(sources, manager)
File "mypy/build.py", line 1841, in dispatch
process_graph(graph, manager)
File "mypy/build.py", line 2091, in process_graph
process_stale_scc(graph, scc, manager)
File "mypy/build.py", line 2194, in process_stale_scc
graph[id].type_check_first_pass()
File "mypy/build.py", line 1753, in type_check_first_pass
self.type_checker.check_first_pass()
File "mypy/checker.py", line 194, in check_first_pass
self.accept(d)
File "mypy/checker.py", line 282, in accept
stmt.accept(self)
File "mypy/nodes.py", line 519, in accept
return visitor.visit_func_def(self)
File "mypy/checker.py", line 530, in visit_func_def
self.check_func_item(defn, name=defn.name())
File "mypy/checker.py", line 590, in check_func_item
self.check_func_def(defn, typ, name)
File "mypy/checker.py", line 750, in check_func_def
self.accept(item.body)
File "mypy/checker.py", line 282, in accept
stmt.accept(self)
File "mypy/nodes.py", line 773, in accept
return visitor.visit_block(self)
File "mypy/checker.py", line 1304, in visit_block
self.accept(s)
File "mypy/checker.py", line 282, in accept
stmt.accept(self)
File "mypy/nodes.py", line 817, in accept
return visitor.visit_assignment_stmt(self)
File "mypy/checker.py", line 1311, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "mypy/checker.py", line 1339, in check_assignment
infer_lvalue_type)
File "mypy/checker.py", line 1589, in check_assignment_to_multiple_lvalues
self.check_multi_assignment(lvalues, rvalue, context, infer_lvalue_type)
File "mypy/checker.py", line 1630, in check_multi_assignment
context, undefined_rvalue, infer_lvalue_type)
File "mypy/checker.py", line 1650, in check_multi_assignment_from_tuple
reinferred_rvalue_type = self.expr_checker.accept(rvalue, lvalue_type)
File "mypy/checkexpr.py", line 2372, in accept
typ = node.accept(self)
File "mypy/nodes.py", line 1256, in accept
return visitor.visit_call_expr(self)
File "mypy/checkexpr.py", line 266, in visit_call_expr
ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname, object_type)
File "mypy/checkexpr.py", line 503, in check_call_expr_with_callee_type
object_type=object_type)[0]
File "mypy/checkexpr.py", line 563, in check_call
callee, context)
File "mypy/checkexpr.py", line 752, in infer_function_type_arguments_using_context
erased_ctx = replace_meta_vars(ctx, ErasedType())
File "mypy/erasetype.py", line 94, in replace_meta_vars
return t.accept(TypeVarEraser(lambda id: id.is_meta_var(), target_type))
File "mypy/types.py", line 982, in accept
return visitor.visit_tuple_type(self)
File "mypy/types.py", line 1554, in visit_tuple_type
return TupleType(self.translate_types(t.items),
File "mypy/types.py", line 1574, in translate_types
return [t.accept(self) for t in types]
File "mypy/types.py", line 1574, in <listcomp>
return [t.accept(self) for t in types]
File "mypy/types.py", line 982, in accept
return visitor.visit_tuple_type(self)
File "mypy/types.py", line 1554, in visit_tuple_type
return TupleType(self.translate_types(t.items),
File "mypy/types.py", line 1574, in translate_types
return [t.accept(self) for t in types]
File "mypy/types.py", line 1574, in <listcomp>
return [t.accept(self) for t in types]
AttributeError: 'NoneType' object has no attribute 'accept'
/tmp/test.py:13: : note: use --pdb to drop into pdb

# Fixed code
def infer_function_type_arguments_using_context(
    self, callable: CallableType, error_context: Context
) -> CallableType:
    """Unify callable return type to type context to infer type vars.

    For example, if the return type is set[t] where 't' is a type variable
    of callable, and if the context is set[int], return callable modified
    by substituting 't' with 'int'.
    """
    ctx = self.type_context[-1]
    if not ctx:
        return callable
    # The return type may have references to type metavariables that
    # we are inferring right now. We must consider them as indeterminate
    # and they are not potential results; thus we replace them with the
    # special ErasedType type. On the other hand, class type variables are
    # valid results.
    erased_ctx = replace_meta_vars(ctx, ErasedType())
    ret_type = callable.ret_type
    if isinstance(ret_type, TypeVarType):
        if ret_type.values or (not isinstance(ctx, Instance) or not ctx.args):
            # The return type is a type variable. If it has values, we can't easily restrict
            # type inference to conform to the valid values. If it's unrestricted, we could
            # infer a too general type for the type variable if we use context, and this could
            # result in confusing and spurious type errors elsewhere.
            #
            # Give up and just use function arguments for type inference. As an exception,
            # if the context is a generic instance type, actually use it as context, as
            # this *seems* to usually be the reasonable thing to do.
            #
            # See also github issues #462 and #360.
            ret_type = NoneTyp()
    args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
    # Only substitute non-Uninhabited and non-erased types.
    new_args = []  # type: List[Optional[Type]]
    for arg in args:
        if has_uninhabited_component(arg) or has_erased_component(arg):
            new_args.append(None)
        else:
            new_args.append(arg)
    return self.apply_generic_arguments(callable, new_args, error_context)