diff --git a/mypy/semanal.py b/mypy/semanal.py index bebabfd3233c..ebd9515d832d 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -5332,6 +5332,7 @@ def visit_expression_stmt(self, s: ExpressionStmt) -> None: s.expr.accept(self) def visit_return_stmt(self, s: ReturnStmt) -> None: + old = self.statement self.statement = s if not self.is_func_scope(): self.fail('"return" outside function', s) @@ -5339,6 +5340,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> None: self.fail('"return" not allowed in except* block', s, serious=True) if s.expr: s.expr.accept(self) + self.statement = old def visit_raise_stmt(self, s: RaiseStmt) -> None: self.statement = s diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 62f538260fff..23dbe2bc07af 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -9258,3 +9258,18 @@ main:5: note: def __eq__(self, other: object) -> bool: main:5: note: if not isinstance(other, C): main:5: note: return NotImplemented main:5: note: return + +[case testLambdaInAttributeCallValue] +# https://github.com/python/mypy/issues/19632 +import foo + +def nop(fn: object) -> foo.Bar: + return foo.Bar() + +class Bar: + foo: foo.Bar = nop( + lambda: 0 + ) +[file foo.py] +class Bar: + ...