[python] PY-83029 fix compatibility inspection tests

`testBreakInFinallyBlock` and `testReturnInFinallyBlock`
were testing vs. latest language level, so apart from
the compatibility warning (for which these tests are
intended) - a corresponding syntax error was captured

I made these tests to use a specific language level
so they only verify the correctness of the compatibility
inspection and introduced four tests in `PythonHighlightingTest`
to capture the syntax error


(cherry picked from commit 254570dc4d5022176590377eaf8ea6a08917937a)

IJ-MR-182362

GitOrigin-RevId: fb9c92923820b370672451c5753ee24bdd5a4570
This commit is contained in:
Pavel Karateev
2025-12-02 11:21:12 +00:00
committed by intellij-monorepo-bot
parent b257c225d2
commit 9285fded89
6 changed files with 57 additions and 12 deletions
@@ -0,0 +1,5 @@
while True:
try:
print("a")
finally:
<error descr="Python version 3.14 does not support 'break' inside 'finally' clause">break</error>
@@ -0,0 +1,5 @@
while True:
try:
print("a")
finally:
break
@@ -0,0 +1,5 @@
def foo():
try:
pass
finally:
<error descr="Python version 3.14 does not support 'return' inside 'finally' clause">return</error>
@@ -0,0 +1,5 @@
def foo():
try:
pass
finally:
return
@@ -97,6 +97,26 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(LanguageLevel.PYTHON38, false, false);
}
// PY-80237
public void testBreakInFinallyBlockBefore314() {
doTest(LanguageLevel.PYTHON313, false, false);
}
// PY-80237
public void testBreakInFinallyBlock() {
doTest(LanguageLevel.PYTHON314, false, false);
}
// PY-80237
public void testReturnInFinallyBlockBefore314() {
doTest(LanguageLevel.PYTHON313, false, false);
}
// PY-80237
public void testReturnInFinallyBlock() {
doTest(LanguageLevel.PYTHON314, false, false);
}
public void testReturnWithArgumentsInGenerator() {
doTest();
}
@@ -16,29 +16,34 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyInspectionTestCase;
import com.jetbrains.python.psi.LanguageLevel;
import org.jetbrains.annotations.NotNull;
public class Py3CompatibilityInspectionTest extends PyInspectionTestCase {
// PY-80237
public void testBreakInFinallyBlock() {
doTestByText("""
while True:
try:
print("a")
finally:
<error descr="Python version 3.15 does not support 'break' inside 'finally' clause">break</error>"""
runWithLanguageLevel(
LanguageLevel.PYTHON313,
() -> doTestByText("""
while True:
try:
print("a")
finally:
<warning descr="Python versions 3.14, 3.15 do not support 'break' inside 'finally' clause">break</warning>""")
);
}
// PY-80237
public void testReturnInFinallyBlock() {
doTestByText("""
def foo():
try:
pass
finally:
<error descr="Python version 3.15 does not support 'return' inside 'finally' clause">return</error>"""
runWithLanguageLevel(
LanguageLevel.PYTHON313,
() -> doTestByText("""
def foo():
try:
pass
finally:
<warning descr="Python versions 3.14, 3.15 do not support 'return' inside 'finally' clause">return</warning>""")
);
}