From db6d06207c36cf7a706b26c063cabd4193aae137 Mon Sep 17 00:00:00 2001 From: Andrey Vokin Date: Tue, 14 Jan 2025 22:29:14 +0100 Subject: [PATCH] PY-77891 "Unused import" inspection is inconsistent in nightly The logic for disabling the unused warning on unresolved imports was dependent on the visit order. Moving sustracting unresolved imports to the end of computation fixes the problem. (cherry picked from commit adeb85e59c17261a5bf9f64dfb4a7836a2403f43) IJ-CR-153189 GitOrigin-RevId: fac4173a0fac5b29be9301f9d5f04fed08cceff1 --- .../PyUnresolvedReferencesVisitor.java | 6 ++++-- .../unusedImport/unresolvedModule/test.py | 4 ++++ .../inspections/PyUnusedImportTest.java | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 python/testData/inspections/unusedImport/unresolvedModule/test.py diff --git a/python/python-psi-impl/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesVisitor.java b/python/python-psi-impl/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesVisitor.java index 6a120f14bf9e..232ae28255e4 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesVisitor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesVisitor.java @@ -67,6 +67,7 @@ public abstract class PyUnresolvedReferencesVisitor extends PyInspectionVisitor private final Set myAllImports = Collections.synchronizedSet(new HashSet<>()); private final Set myImportsInsideGuard = Collections.synchronizedSet(new HashSet<>()); private final Set myUsedImports = Collections.synchronizedSet(new HashSet<>()); + private final Set myUnresolvedImports = Collections.synchronizedSet(new HashSet<>()); private final ImmutableSet myIgnoredIdentifiers; private final PyInspection myInspection; private volatile Boolean myIsEnabled = null; @@ -226,8 +227,8 @@ public abstract class PyUnresolvedReferencesVisitor extends PyInspectionVisitor registerUnresolvedReferenceProblem(node, reference, severity); } // don't highlight unresolved imports as unused - if (node.getParent() instanceof PyImportElement) { - myAllImports.remove(node.getParent()); + if (node.getParent() instanceof PyImportElement importElement) { + myUnresolvedImports.add(importElement); } } else if (reference instanceof PyImportReference && @@ -605,6 +606,7 @@ public abstract class PyUnresolvedReferencesVisitor extends PyInspectionVisitor Set unusedImports = new HashSet<>(getAllImports()); unusedImports.removeAll(getUsedImports()); + unusedImports.removeAll(myUnresolvedImports); // Remove those unsed, that are reported to be skipped by extension points final Set unusedImportToSkip = new HashSet<>(); diff --git a/python/testData/inspections/unusedImport/unresolvedModule/test.py b/python/testData/inspections/unusedImport/unresolvedModule/test.py new file mode 100644 index 000000000000..68badb56051c --- /dev/null +++ b/python/testData/inspections/unusedImport/unresolvedModule/test.py @@ -0,0 +1,4 @@ +import unresolved1 +import unresolved2 +import unresolved3 +import os \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnusedImportTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnusedImportTest.java index fe4d4f6d17b5..90e8a76e8d04 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnusedImportTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnusedImportTest.java @@ -37,6 +37,25 @@ public class PyUnusedImportTest extends PyTestCase { doTest("test1.py"); } + public void testUnresolvedModule() { + doTest("test.py"); + //myFixture.configureByText("test.py", """ + // import unresolved1 + // import unresolved2 + // import unresolved3 + // """); + //myFixture.enableInspections(PyUnresolvedReferencesInspection.class); + // + // + //final ExpectedHighlightingData data = new ExpectedHighlightingData(myFixture.getEditor().getDocument(), true, true, false); + //data.init(); + // + //myFixture.type("\n"); + //myFixture.type("\b"); + // + //((CodeInsightTestFixtureImpl)myFixture).collectAndCheckHighlighting(data); + } + //PY-20075 public void testMultipleSubmodules() { doTest("test1.py");