Skip to content

Allow lambdas in except* clauses #18620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6134,7 +6134,8 @@ def analyze_comp_for_2(self, expr: GeneratorExpr | DictionaryComprehension) -> N

def visit_lambda_expr(self, expr: LambdaExpr) -> None:
self.analyze_arg_initializers(expr)
self.analyze_function_body(expr)
with self.inside_except_star_block_set(False, entering_loop=False):
self.analyze_function_body(expr)

def visit_conditional_expr(self, expr: ConditionalExpr) -> None:
expr.if_expr.accept(self)
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-python311.test
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,24 @@ def foo():
return # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testLambdaInExceptStarBlock]
# flags: --python-version 3.11
def foo():
try:
pass
except* Exception:
x = lambda: 0
return lambda: 0 # E: "return" not allowed in except* block

def loop():
while True:
try:
pass
except* Exception:
x = lambda: 0
return lambda: 0 # E: "return" not allowed in except* block
[builtins fixtures/exception.pyi]

[case testRedefineLocalWithinExceptStarTryClauses]
# flags: --allow-redefinition
def fn_str(_: str) -> int: ...
Expand Down