# Buggy code
def visit_lambda_expr(self, e: LambdaExpr) -> Type:
    """Type check lambda expression."""
    self.chk.check_default_args(e, body_is_trivial=False)
    inferred_type, type_override = self.infer_lambda_type_using_context(e)
    if not inferred_type:
        self.chk.return_types.append(AnyType(TypeOfAny.special_form))
        # Type check everything in the body except for the final return
        # statement (it can contain tuple unpacking before return).
        for stmt in e.body.body[:-1]:
            stmt.accept(self.chk)
        # Only type check the return expression, not the return statement.
        # This is important as otherwise the following statements would be
        # considered unreachable. There's no useful type context.
        ret_type = self.accept(e.expr(), allow_none_return=True)
        fallback = self.named_type("builtins.function")
        self.chk.return_types.pop()
        return callable_type(e, fallback, ret_type)
    else:
        # Type context available.
        self.chk.return_types.append(inferred_type.ret_type)
        self.chk.check_func_item(e, type_override=type_override)
        if e.expr() not in self.chk.type_map:
            self.accept(e.expr(), allow_none_return=True)
        ret_type = self.chk.type_map[e.expr()]
        if isinstance(get_proper_type(ret_type), NoneType):
            # For "lambda ...: None", just use type from the context.
            # Important when the context is Callable[..., None] which
            # really means Void. See #1425.
            self.chk.return_types.pop()
            return inferred_type
        self.chk.return_types.pop()
        return replace_callable_return_type(inferred_type, ret_type)


# Traceback (AssertionError)
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/jukka/src/mypy/mypy/__main__.py", line 12, in <module>
main(None, sys.stdout, sys.stderr)
File "/Users/jukka/src/mypy/mypy/main.py", line 89, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/Users/jukka/src/mypy/mypy/build.py", line 167, in build
sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr, extra_plugins
File "/Users/jukka/src/mypy/mypy/build.py", line 235, in _build
graph = dispatch(sources, manager, stdout)
File "/Users/jukka/src/mypy/mypy/build.py", line 2620, in dispatch
process_graph(graph, manager)
File "/Users/jukka/src/mypy/mypy/build.py", line 2929, in process_graph
process_stale_scc(graph, scc, manager)
File "/Users/jukka/src/mypy/mypy/build.py", line 3028, in process_stale_scc
graph[id].type_check_first_pass()
File "/Users/jukka/src/mypy/mypy/build.py", line 2119, in type_check_first_pass
self.type_checker().check_first_pass()
File "/Users/jukka/src/mypy/mypy/checker.py", line 292, in check_first_pass
self.accept(d)
File "/Users/jukka/src/mypy/mypy/checker.py", line 399, in accept
stmt.accept(self)
File "/Users/jukka/src/mypy/mypy/nodes.py", line 939, in accept
return visitor.visit_class_def(self)
File "/Users/jukka/src/mypy/mypy/checker.py", line 1719, in visit_class_def
self.accept(defn.defs)
File "/Users/jukka/src/mypy/mypy/checker.py", line 399, in accept
stmt.accept(self)
File "/Users/jukka/src/mypy/mypy/nodes.py", line 1004, in accept
return visitor.visit_block(self)
File "/Users/jukka/src/mypy/mypy/checker.py", line 1970, in visit_block
self.accept(s)
File "/Users/jukka/src/mypy/mypy/checker.py", line 399, in accept
stmt.accept(self)
File "/Users/jukka/src/mypy/mypy/nodes.py", line 1062, in accept
return visitor.visit_assignment_stmt(self)
File "/Users/jukka/src/mypy/mypy/checker.py", line 2004, in visit_assignment_stmt
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
File "/Users/jukka/src/mypy/mypy/checker.py", line 2140, in check_assignment
rvalue_type = self.expr_checker.accept(rvalue)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3646, in accept
typ = node.accept(self)
File "/Users/jukka/src/mypy/mypy/nodes.py", line 1845, in accept
return visitor.visit_lambda_expr(self)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3251, in visit_lambda_expr
ret_type = self.accept(e.expr(), allow_none_return=True)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 3642, in accept
typ = self.visit_call_expr(node, allow_none_return=True)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 263, in visit_call_expr
return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 299, in visit_call_expr_inner
self.try_infer_partial_type(e)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 573, in try_infer_partial_type
var = self.get_partial_self_var(e.callee.expr)
File "/Users/jukka/src/mypy/mypy/checkexpr.py", line 550, in get_partial_self_var
info = self.chk.scope.enclosing_class()
File "/Users/jukka/src/mypy/mypy/checker.py", line 4917, in enclosing_class
assert top, "This method must be called from inside a function"
AssertionError: This method must be called from inside a function

# Fixed code
def visit_lambda_expr(self, e: LambdaExpr) -> Type:
    """Type check lambda expression."""
    self.chk.check_default_args(e, body_is_trivial=False)
    inferred_type, type_override = self.infer_lambda_type_using_context(e)
    if not inferred_type:
        self.chk.return_types.append(AnyType(TypeOfAny.special_form))
        # Type check everything in the body except for the final return
        # statement (it can contain tuple unpacking before return).
        with self.chk.scope.push_function(e):
            for stmt in e.body.body[:-1]:
                stmt.accept(self.chk)
            # Only type check the return expression, not the return statement.
            # This is important as otherwise the following statements would be
            # considered unreachable. There's no useful type context.
            ret_type = self.accept(e.expr(), allow_none_return=True)
        fallback = self.named_type("builtins.function")
        self.chk.return_types.pop()
        return callable_type(e, fallback, ret_type)
    else:
        # Type context available.
        self.chk.return_types.append(inferred_type.ret_type)
        self.chk.check_func_item(e, type_override=type_override)
        if e.expr() not in self.chk.type_map:
            # TODO: return expression must be accepted before exiting function scope.
            self.accept(e.expr(), allow_none_return=True)
        ret_type = self.chk.type_map[e.expr()]
        if isinstance(get_proper_type(ret_type), NoneType):
            # For "lambda ...: None", just use type from the context.
            # Important when the context is Callable[..., None] which
            # really means Void. See #1425.
            self.chk.return_types.pop()
            return inferred_type
        self.chk.return_types.pop()
        return replace_callable_return_type(inferred_type, ret_type)