Skip to content

Commit 7c0c4b4

Browse files
authored
Allow lambdas in except* clauses (#18620)
Fixes #18618
1 parent 5249654 commit 7c0c4b4

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
@@ -6143,7 +6143,8 @@ def analyze_comp_for_2(self, expr: GeneratorExpr | DictionaryComprehension) -> N
61436143

61446144
def visit_lambda_expr(self, expr: LambdaExpr) -> None:
61456145
self.analyze_arg_initializers(expr)
6146-
self.analyze_function_body(expr)
6146+
with self.inside_except_star_block_set(False, entering_loop=False):
6147+
self.analyze_function_body(expr)
61476148

61486149
def visit_conditional_expr(self, expr: ConditionalExpr) -> None:
61496150
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)