Files
openide/python/testData/inspections/PyUnreachableCodeInspection/Unreachable.py
Aleksandr.Govenko 6659947437 PY-51564 "code is unreachable" warning does not consider that context managers can abort
Added PyWithContextExitInstruction that works as 'except' node in try-except statements, but for 'with context-manager' statements. This allows to see when it can recover from exception by checking the type of `contextlib.AbstractContextManager`

Merge-request: IJ-MR-152213
Merged-by: Aleksandr Govenko <aleksandr.govenko@jetbrains.com>

GitOrigin-RevId: fd5a139de31d47136cc86b4a7e090a20b73b8744
2025-02-14 13:18:20 +00:00

134 lines
1.9 KiB
Python

import unittest
def f():
return
<warning descr="This code is unreachable">a = 1</warning>
def f():
if b:
return
a = 1 # pass
def f():
raise Exception()
<warning descr="This code is unreachable">a = 1</warning>
def f():
for x in []:
break
<warning descr="This code is unreachable">a = x</warning>
def f():
for x in []:
if x == 0:
break
a = 1 # pass
def f():
for x in []:
continue
<warning descr="This code is unreachable">a = 1</warning>
def f():
for x in []:
raise Exception()
<warning descr="This code is unreachable">if 1:
pass</warning>
def f():
if 1:
return
<warning descr="This code is unreachable">print "x"</warning>
def f():
try:
raise KeyboardInterrupt
finally:
print 'test'
class MyTestCase(unittest.TestCase):
def test_something(self):
with self.assertRaises():
raise Foo
foo() # pass
# PY-3532
def f():
import sys
f = lambda: sys.exit() #pass
foo = 3
return f, foo
# PY-4149
def f():
try:
pass
finally: #pass
pass
# PY-4208
def f(g):
try:
raise ValueError
finally:
g()
<warning descr="This code is unreachable">g()</warning>
# PY-5266
def f(g):
x = 0
try:
x = g()
except Exception:
try:
x = 2
return
except Exception:
raise
print(x) #pass
# PY-6159
def f(c):
while c:
break #pass
else:
x = 1
# PY-6062
def f(x):
for _ in [1, 2]:
for _ in [3, 4]:
pass
else:
break
else:
return
print(x) #pass
# PY-6062
def f(x):
for _ in [1, 2]:
while x:
pass
else:
break
else:
return
print(x) #pass