Skip to content

Commit a9f8ced

Browse files
committed
Keep track of whether a try/except block can fall off its end.
1 parent 9d14b23 commit a9f8ced

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mypy/checker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,16 +1866,18 @@ def type_check_raise(self, e: Node, s: RaiseStmt) -> None:
18661866

18671867
def visit_try_stmt(self, s: TryStmt) -> Type:
18681868
"""Type check a try statement."""
1869+
broken = True
18691870
with self.binder.frame_context():
1870-
with self.binder.frame_context(fall_through=True, clear_breaking=True):
1871+
with self.binder.frame_context(fall_through=True, clear_breaking=True) as frame:
18711872
self.binder.try_frames.add(len(self.binder.frames) - 2)
18721873
self.accept(s.body)
18731874
self.binder.try_frames.remove(len(self.binder.frames) - 2)
18741875
if s.else_body:
18751876
self.accept(s.else_body)
1877+
broken = broken and frame.broken
18761878

18771879
for i in range(len(s.handlers)):
1878-
with self.binder.frame_context(clear_breaking=True):
1880+
with self.binder.frame_context(clear_breaking=True) as frame:
18791881
if s.types[i]:
18801882
t = self.visit_except_handler_test(s.types[i])
18811883
if s.vars[i]:
@@ -1899,9 +1901,12 @@ def visit_try_stmt(self, s: TryStmt) -> Type:
18991901
var = cast(Var, s.vars[i].node)
19001902
var.type = DeletedType(source=source)
19011903
self.binder.cleanse(s.vars[i])
1904+
broken = broken and frame.broken
19021905

19031906
if s.finally_body:
19041907
self.accept(s.finally_body)
1908+
if broken:
1909+
self.binder.breaking_out = True
19051910

19061911
def visit_except_handler_test(self, n: Node) -> Type:
19071912
"""Type check an exception handler test clause."""

test-data/unit/check-isinstance.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,17 @@ while 1:
837837
x = 'a' # Note: no error because unreachable code
838838
[builtins fixtures/isinstancelist.py]
839839

840+
[case testUnreachableCode2]
841+
x = 1
842+
while 1:
843+
try:
844+
pass
845+
except:
846+
continue
847+
else:
848+
continue
849+
x + 'a'
850+
[builtins fixtures/isinstance.py]
840851

841852
[case testIsinstanceAnd]
842853
class A:

0 commit comments

Comments
 (0)