PY-91903 Adapt type inference tests to disabled registry flag

The 262 branch disables better control-flow inference by default. Skip tests that require the new inference and adjust the recursion-prevention expectation so the unknown-value test remains valid in both registry modes.


(cherry picked from commit 491ed19993120c2e09bc550790a9493277c0cb1b)

IJ-MR-221796

GitOrigin-RevId: 3e4822b14f96f2b2ab34bb287238b2c0e007f1f8
This commit is contained in:
Andrey Vokin
2026-09-05 09:43:44 +00:00
committed by intellij-monorepo-bot
parent 979fc49c5d
commit 0cbcaf3e49
3 changed files with 61 additions and 43 deletions
@@ -5,6 +5,7 @@ import com.intellij.idea.TestFor;
import com.intellij.lang.FileASTNode;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.PsiTestUtil;
@@ -615,6 +616,8 @@ public class Py3UnresolvedReferencesInspectionTest extends PyInspectionTestCase
// PY-89245
public void testFlakyLoop() {
if (!Registry.is("python.use.better.control.flow.type.inference")) return;
doTestByText("""
class ListNode:
def __init__(self, val=0, next=None):
@@ -2,6 +2,7 @@
package com.jetbrains.python.types
import com.intellij.idea.TestFor
import com.intellij.openapi.util.registry.Registry
import com.jetbrains.python.fixtures.PyCodeInsightTestCase
import com.jetbrains.python.psi.LanguageLevel
import org.junit.jupiter.api.Disabled
@@ -273,49 +274,57 @@ class PyInferenceMiscTypeTest : PyCodeInsightTestCase() {
@Test
@TestFor(issues = ["PY-76659"])
fun `class chain fixed point in while loop`() = test("""
class A:
def bar() -> "B":
return B()
class B:
def bar() -> "C":
return C()
class C:
def bar() -> "D":
return D()
class D:
def bar() -> A:
return A()
fun `class chain fixed point in while loop`() {
if (!Registry.`is`("python.use.better.control.flow.type.inference")) return
def foo(b):
x = A()
while b:
x = x.bar()
test("""
class A:
def bar() -> "B":
return B()
class B:
def bar() -> "C":
return C()
class C:
def bar() -> "D":
return D()
class D:
def bar() -> A:
return A()
expr = x
# └ TYPE A | B | C | D
""")
def foo(b):
x = A()
while b:
x = x.bar()
expr = x
# └ TYPE A | B | C | D
""")
}
@Test
@TestFor(issues = ["PY-76659"])
fun `types in loop compute fast`() = test("""
def is_empty(x: int, y: int) -> bool:
...
fun `types in loop compute fast`() {
if (!Registry.`is`("python.use.better.control.flow.type.inference")) return
def drop_grain() -> None:
x, y = 500, 0
test("""
def is_empty(x: int, y: int) -> bool:
...
while True:
if is_empty(x, y):
x, y = x + 1, y
elif is_empty(x, y):
x, y = x, y
elif is_empty((expr := x), y):
# └ TYPE Literal[500] | int
x, y = x, y
elif not is_empty(x, y):
break
""")
def drop_grain() -> None:
x, y = 500, 0
while True:
if is_empty(x, y):
x, y = x + 1, y
elif is_empty(x, y):
x, y = x, y
elif is_empty((expr := x), y):
# └ TYPE Literal[500] | int
x, y = x, y
elif not is_empty(x, y):
break
""")
}
// TODO investigate the inference flakyness
// The test is disabled because it's flaky: sometimes different types are inferred in the code analysis and user-initiated contexts.
@@ -2,6 +2,7 @@
package com.jetbrains.python.types
import com.intellij.idea.TestFor
import com.intellij.openapi.util.registry.Registry
import com.jetbrains.python.fixtures.PyCodeInsightTestCase
import com.jetbrains.python.psi.LanguageLevel
import org.junit.jupiter.api.Nested
@@ -44,13 +45,18 @@ class PyTypeAliasAndFormsTest : PyCodeInsightTestCase() {
@Test
@TestFor(issues = ["PY-7058"])
fun `type of an unknown value is Unknown`() = test(
"""
def f(x):
expr = type(x)
# └ TYPE Unknown
""",
)
fun `type of an unknown value is Unknown`() {
test(
defaultTestOptions.copy(
assertRecursionPrevention = Registry.`is`("python.use.better.control.flow.type.inference"),
),
"""
def f(x):
expr = type(x)
# └ TYPE Unknown
""",
)
}
@Test
fun `Type of union of class objects`() = test("""