Skip to content

Commit cb77a9b

Browse files
authored
Reset to previous statement when leaving return in semanal (#19642)
Unlike other statements, return can be found in lambdas, and so is not immediately followed by another statement to check or EOF. We need to restore the previous value to continue checking it. Fixes #19632
1 parent 74927d5 commit cb77a9b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5332,13 +5332,15 @@ def visit_expression_stmt(self, s: ExpressionStmt) -> None:
53325332
s.expr.accept(self)
53335333

53345334
def visit_return_stmt(self, s: ReturnStmt) -> None:
5335+
old = self.statement
53355336
self.statement = s
53365337
if not self.is_func_scope():
53375338
self.fail('"return" outside function', s)
53385339
if self.return_stmt_inside_except_star_block:
53395340
self.fail('"return" not allowed in except* block', s, serious=True)
53405341
if s.expr:
53415342
s.expr.accept(self)
5343+
self.statement = old
53425344

53435345
def visit_raise_stmt(self, s: RaiseStmt) -> None:
53445346
self.statement = s

test-data/unit/check-classes.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9258,3 +9258,18 @@ main:5: note: def __eq__(self, other: object) -> bool:
92589258
main:5: note: if not isinstance(other, C):
92599259
main:5: note: return NotImplemented
92609260
main:5: note: return <logic to compare two C instances>
9261+
9262+
[case testLambdaInAttributeCallValue]
9263+
# https://github.com/python/mypy/issues/19632
9264+
import foo
9265+
9266+
def nop(fn: object) -> foo.Bar:
9267+
return foo.Bar()
9268+
9269+
class Bar:
9270+
foo: foo.Bar = nop(
9271+
lambda: 0
9272+
)
9273+
[file foo.py]
9274+
class Bar:
9275+
...

0 commit comments

Comments
 (0)