[python] PY-86190: Enable missing interpreter warning on empty file.

PR: https://github.com/JetBrains/intellij-community/pull/3341

GitOrigin-RevId: 04c927e2c37a9a8c9ae0d5612efec4f1ea9b5b05
This commit is contained in:
chbndrhnns
2025-12-15 19:04:20 +00:00
committed by intellij-monorepo-bot
parent 7e88d9b28b
commit b9dab0fe25
2 changed files with 25 additions and 4 deletions
@@ -27,6 +27,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.resolve.FileContextUtil;
import com.intellij.util.ObjectUtils;
import com.jetbrains.python.psi.PyElementVisitor;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.resolve.PyResolveContext;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.NotNull;
@@ -80,7 +81,7 @@ public abstract class PyInspectionVisitor extends PyElementVisitor {
protected final void registerProblem(@Nullable PsiElement element,
@NotNull @InspectionMessage String message) {
if (element == null || element.getTextLength() == 0) {
if (!canRegisterProblem(element)) {
return;
}
if (myHolder != null) {
@@ -91,7 +92,7 @@ public abstract class PyInspectionVisitor extends PyElementVisitor {
protected final void registerProblem(@Nullable PsiElement element,
@NotNull @InspectionMessage String message,
LocalQuickFix @NotNull ... quickFixes) {
if (element == null || element.getTextLength() == 0) {
if (!canRegisterProblem(element)) {
return;
}
if (myHolder != null) {
@@ -102,7 +103,7 @@ public abstract class PyInspectionVisitor extends PyElementVisitor {
protected final void registerProblem(@Nullable PsiElement element,
@NotNull @InspectionMessage String message,
@NotNull ProblemHighlightType type) {
if (element == null || element.getTextLength() == 0) {
if (!canRegisterProblem(element)) {
return;
}
if (myHolder != null) {
@@ -111,6 +112,18 @@ public abstract class PyInspectionVisitor extends PyElementVisitor {
}
}
private static boolean canRegisterProblem(@Nullable PsiElement element) {
if (element == null) {
return false;
}
if (element.getTextLength() > 0) {
return true;
}
return element instanceof PyFile;
}
/**
* The most full-blown version.
*
@@ -14,6 +14,14 @@ class PyInterpreterInspectionTest : PyTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor? = ourPyLatestDescriptor
fun testNoInterpreterConfiguredShowsProblem() {
assertInterpreterWarning("test.py", "print('hello')\n")
}
fun testNoInterpreterConfiguredShowsProblemInEmptyFile() {
assertInterpreterWarning("__init__.py", "")
}
private fun assertInterpreterWarning(fileName: String, content: String) {
val project = myFixture.project
val module = myFixture.module
@@ -29,7 +37,7 @@ class PyInterpreterInspectionTest : PyTestCase() {
val expectedMsg = PyPsiBundle.message("INSP.interpreter.no.python.interpreter.configured.for.module")
myFixture.configureByText("test.py", "print('hello')\n")
myFixture.configureByText(fileName, content)
myFixture.enableInspections(PyInterpreterInspection::class.java)
val highlights = myFixture.doHighlighting()