From d2af13aa3a28fc041e8636a02ffa0ac74f38f74d Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 21 Jan 2014 02:13:02 +0400 Subject: [PATCH] PY-6955 Unused import not greyed out if also failing --- .../PyUnresolvedReferencesInspection.java | 4 +++ .../FromPackageImportBuiltin/a.py | 2 ++ .../UnusedUnresolvedNameImported/a.py | 1 + .../UnusedUnresolvedNameImported/my_module.py | 2 ++ .../a.py | 2 ++ .../my_module.py | 2 ++ .../a.py | 4 +++ .../my_package/__init__.py | 1 + .../my_package/my_module.py | 2 ++ .../importFunction.py | 2 ++ .../unresolvedImport.py | 2 ++ .../unusedUnresolvedModuleImported.py | 1 + .../unusedUnresolvedPackageImported.py | 1 + .../python/fixtures/PyInspectionTestCase.java | 3 +++ .../PyUnresolvedReferencesInspectionTest.java | 25 +++++++++++++++++++ 15 files changed, 54 insertions(+) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/a.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/my_module.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/a.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/my_module.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/a.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/__init__.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/my_module.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedModuleImported.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedPackageImported.py diff --git a/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java b/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java index 998530a016b3..7b71bdb7cfde 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java @@ -984,6 +984,10 @@ public class PyUnresolvedReferencesInspection extends PyInspection { final PyImportElement importElement = (PyImportElement)unusedImport; final PsiElement element = importElement.resolve(); if (element == null) { + if (importElement.getImportedQName() != null) { + //Mark import as unused even if it can't be resolved + result.add(importElement.getParent()); + } continue; } if (dunderAll != null && dunderAll.contains(importElement.getVisibleName())) { diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/FromPackageImportBuiltin/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/FromPackageImportBuiltin/a.py index a66445c7362c..8880a48e3db9 100644 --- a/python/testData/inspections/PyUnresolvedReferencesInspection/FromPackageImportBuiltin/a.py +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/FromPackageImportBuiltin/a.py @@ -1 +1,3 @@ from importSource import len + +len() diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/a.py new file mode 100644 index 000000000000..b0ad821f96b9 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/a.py @@ -0,0 +1 @@ +from my_module import eggs diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/my_module.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/my_module.py new file mode 100644 index 000000000000..51d8eaddb572 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImported/my_module.py @@ -0,0 +1,2 @@ +def my_func(): + pass \ No newline at end of file diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/a.py new file mode 100644 index 000000000000..5e1c928b579d --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/a.py @@ -0,0 +1,2 @@ +from my_module import eggs +from my_module import eggs diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/my_module.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/my_module.py new file mode 100644 index 000000000000..51d8eaddb572 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedUnresolvedNameImportedSeveralTimes/my_module.py @@ -0,0 +1,2 @@ +def my_func(): + pass \ No newline at end of file diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/a.py new file mode 100644 index 000000000000..767f9bf68131 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/a.py @@ -0,0 +1,4 @@ +from my_package.my_module import eggs +from my_package.my_module import eggs + +eggs() diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/__init__.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/__init__.py new file mode 100644 index 000000000000..a5f9f02f5908 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/__init__.py @@ -0,0 +1 @@ +__author__ = 'Ilya.Kazakevich' diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/my_module.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/my_module.py new file mode 100644 index 000000000000..51d8eaddb572 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UsedUnresolvedNameImportedSeveralTimes/my_package/my_module.py @@ -0,0 +1,2 @@ +def my_func(): + pass \ No newline at end of file diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/importFunction.py b/python/testData/inspections/PyUnresolvedReferencesInspection/importFunction.py index 2bfbaa3d0ee9..75ddd5c94722 100644 --- a/python/testData/inspections/PyUnresolvedReferencesInspection/importFunction.py +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/importFunction.py @@ -1 +1,3 @@ import collections.OrderedDict + +collections.OrderedDict() diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedImport.py b/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedImport.py index e96da63adfe4..91aae4269564 100644 --- a/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedImport.py +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedImport.py @@ -1 +1,3 @@ import wurm + +wurm() diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedModuleImported.py b/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedModuleImported.py new file mode 100644 index 000000000000..3351b1dbceeb --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedModuleImported.py @@ -0,0 +1 @@ +import spam diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedPackageImported.py b/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedPackageImported.py new file mode 100644 index 000000000000..ba929a6c244b --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/unusedUnresolvedPackageImported.py @@ -0,0 +1 @@ +import spam.eggs \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/fixtures/PyInspectionTestCase.java b/python/testSrc/com/jetbrains/python/fixtures/PyInspectionTestCase.java index 4769b09c808e..717a579ce97b 100644 --- a/python/testSrc/com/jetbrains/python/fixtures/PyInspectionTestCase.java +++ b/python/testSrc/com/jetbrains/python/fixtures/PyInspectionTestCase.java @@ -28,6 +28,9 @@ public abstract class PyInspectionTestCase extends PyTestCase { configureInspection(); } + protected void doMultiFileTest() { + doMultiFileTest("a.py"); + } protected void doMultiFileTest(@NotNull String filename) { myFixture.copyDirectoryToProject(getTestDirectory(false), ""); myFixture.configureFromTempProjectFile(filename); diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 7ef9511b0ea2..546795e4fe8f 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -335,6 +335,31 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { doTest(); } + // PY-6955 + public void testUnusedUnresolvedModuleImported() { + doTest(); + } + + // PY-6955 + public void testUnusedUnresolvedNameImported() { + doMultiFileTest(); + } + + // PY-6955 + public void testUnusedUnresolvedNameImportedSeveralTimes() { + doMultiFileTest(); + } + + // PY-6955 + public void testUsedUnresolvedNameImportedSeveralTimes() { + doMultiFileTest(); + } + + // PY-6955 + public void testUnusedUnresolvedPackageImported() { + doTest(); + } + @NotNull @Override protected Class getInspectionClass() {