Skip to content

Commit f60f458

Browse files
authored
Avoid does not return error in lambda (#17294)
Fixes #10520, fixes #15142
1 parent 5059ffd commit f60f458

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4494,7 +4494,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
44944494
is_lambda = isinstance(self.scope.top_function(), LambdaExpr)
44954495
if isinstance(return_type, UninhabitedType):
44964496
# Avoid extra error messages for failed inference in lambdas
4497-
if not is_lambda or not return_type.ambiguous:
4497+
if not is_lambda and not return_type.ambiguous:
44984498
self.fail(message_registry.NO_RETURN_EXPECTED, s)
44994499
return
45004500

test-data/unit/check-unreachable-code.test

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,15 +1502,8 @@ from typing import Callable, NoReturn
15021502
def foo() -> NoReturn:
15031503
raise
15041504

1505-
f = lambda: foo()
1505+
f1 = lambda: foo()
15061506
x = 0 # not unreachable
15071507

1508-
[case testLambdaNoReturnAnnotated]
1509-
# flags: --warn-unreachable
1510-
from typing import Callable, NoReturn
1511-
1512-
def foo() -> NoReturn:
1513-
raise
1514-
1515-
f: Callable[[], NoReturn] = lambda: foo() # E: Return statement in function which does not return # (false positive: https://github.com/python/mypy/issues/17254)
1508+
f2: Callable[[], NoReturn] = lambda: foo()
15161509
x = 0 # not unreachable

0 commit comments

Comments
 (0)