Skip to content

Commit 5f426ea

Browse files
committed
Allow lambda (with implicit return) in except* clauses
1 parent fc991a0 commit 5f426ea

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mypy/semanal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6134,7 +6134,8 @@ def analyze_comp_for_2(self, expr: GeneratorExpr | DictionaryComprehension) -> N
61346134

61356135
def visit_lambda_expr(self, expr: LambdaExpr) -> None:
61366136
self.analyze_arg_initializers(expr)
6137-
self.analyze_function_body(expr)
6137+
with self.inside_except_star_block_set(False, entering_loop=False):
6138+
self.analyze_function_body(expr)
61386139

61396140
def visit_conditional_expr(self, expr: ConditionalExpr) -> None:
61406141
expr.if_expr.accept(self)

test-data/unit/check-python311.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ def foo():
260260
return # E: "return" not allowed in except* block
261261
[builtins fixtures/exception.pyi]
262262

263+
[case testLambdaInExceptStarBlock]
264+
# flags: --python-version 3.11
265+
def foo():
266+
try:
267+
pass
268+
except* Exception:
269+
x = lambda: 0
270+
return lambda: 0 # E: "return" not allowed in except* block
271+
272+
def loop():
273+
while True:
274+
try:
275+
pass
276+
except* Exception:
277+
x = lambda: 0
278+
return lambda: 0 # E: "return" not allowed in except* block
279+
[builtins fixtures/exception.pyi]
280+
263281
[case testRedefineLocalWithinExceptStarTryClauses]
264282
# flags: --allow-redefinition
265283
def fn_str(_: str) -> int: ...

0 commit comments

Comments
 (0)