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");