Skip to content

Commit 2f43c99

Browse files
JukkaLgvanrossum
authored andcommitted
Fix unreachability of elif blocks (python#1455)
Fix python#1454.
1 parent eefab5b commit 2f43c99

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/semanal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2644,7 +2644,7 @@ def infer_reachability_of_if_statement(s: IfStmt,
26442644
# This condition is always true, so all of the remaining
26452645
# elif/else bodies will never be executed.
26462646
for body in s.body[i + 1:]:
2647-
mark_block_unreachable(s.body[i])
2647+
mark_block_unreachable(body)
26482648
if s.else_body:
26492649
mark_block_unreachable(s.else_body)
26502650
break

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

+13
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@ if PY3:
9898
else:
9999
class X(object):
100100
pass
101+
102+
[case testUnreachabilityAndElifPY3]
103+
def f(): pass
104+
PY3 = f()
105+
if PY3:
106+
pass
107+
elif 1:
108+
import nonexistent
109+
1 + ''
110+
else:
111+
import bad_name
112+
1 + ''
113+
[out]

0 commit comments

Comments
 (0)