From b9dab0fe259f73d64ba81287d0fe27a1ef6f3671 Mon Sep 17 00:00:00 2001 From: chbndrhnns Date: Mon, 15 Dec 2025 15:51:56 +0100 Subject: [PATCH] [python] PY-86190: Enable missing interpreter warning on empty file. PR: https://github.com/JetBrains/intellij-community/pull/3341 GitOrigin-RevId: 04c927e2c37a9a8c9ae0d5612efec4f1ea9b5b05 --- .../inspections/PyInspectionVisitor.java | 19 ++++++++++++++++--- .../PyInterpreterInspectionTest.kt | 10 +++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/python/python-psi-impl/src/com/jetbrains/python/inspections/PyInspectionVisitor.java b/python/python-psi-impl/src/com/jetbrains/python/inspections/PyInspectionVisitor.java index bcefd29cb986..2b8b5f732463 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/inspections/PyInspectionVisitor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/inspections/PyInspectionVisitor.java @@ -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. * diff --git a/python/testSrc/com/jetbrains/python/inspections/PyInterpreterInspectionTest.kt b/python/testSrc/com/jetbrains/python/inspections/PyInterpreterInspectionTest.kt index e2f610fa6baf..7e4e9006f1f8 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyInterpreterInspectionTest.kt +++ b/python/testSrc/com/jetbrains/python/inspections/PyInterpreterInspectionTest.kt @@ -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()