Disable unused locals and statement has no effect inspections for Python stubs
authorAndrey Vlasovskikh <andrey.vlasovskikh@jetbrains.com>
Wed, 2 Sep 2015 19:20:41 +0000 (22:20 +0300)
committerAndrey Vlasovskikh <andrey.vlasovskikh@jetbrains.com>
Wed, 2 Sep 2015 19:20:41 +0000 (22:20 +0300)
All parameters in Python stubs are considered used. The '...' statement
common in Python stubs has no effect but it's use is specified by
PEP 484.

python/src/META-INF/python-core.xml
python/src/com/jetbrains/python/pyi/PyiVisitorFilter.java [new file with mode: 0644]
python/testData/pyi/inspections/pyiStatementEffect/PyiStatementEffect.pyi [new file with mode: 0644]
python/testData/pyi/inspections/pyiUnusedParameters/PyiUnusedParameters.pyi [new file with mode: 0644]
python/testSrc/com/jetbrains/python/pyi/PyiInspectionsTest.java

index b5f730fa9e4fa576eb09a6d5b20b6a483c795b26..8ee1429e194c452beb163bcf727217059c728e02 100644 (file)
     <typeProvider implementation="com.jetbrains.python.pyi.PyiTypeProvider"/>
     <pyModuleMembersProvider implementation="com.jetbrains.python.pyi.PyiModuleMembersProvider"/>
     <pyClassMembersProvider implementation="com.jetbrains.python.pyi.PyiClassMembersProvider"/>
+    <visitorFilter language="PythonStub" implementationClass="com.jetbrains.python.pyi.PyiVisitorFilter"/>
 
     <typeProvider implementation="com.jetbrains.python.debugger.PyCallSignatureTypeProvider"/>
     <pyReferenceResolveProvider implementation="com.jetbrains.python.psi.resolve.PythonBuiltinReferenceResolveProvider"/>
diff --git a/python/src/com/jetbrains/python/pyi/PyiVisitorFilter.java b/python/src/com/jetbrains/python/pyi/PyiVisitorFilter.java
new file mode 100644 (file)
index 0000000..c10b3f9
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.pyi;
+
+import com.intellij.psi.PsiFile;
+import com.jetbrains.python.inspections.PyStatementEffectInspection;
+import com.jetbrains.python.inspections.PyUnusedLocalInspection;
+import com.jetbrains.python.inspections.PythonVisitorFilter;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * @author vlan
+ */
+public class PyiVisitorFilter implements PythonVisitorFilter {
+  @Override
+  public boolean isSupported(@NotNull Class visitorClass, @NotNull PsiFile file) {
+    if (visitorClass == PyUnusedLocalInspection.class || visitorClass == PyStatementEffectInspection.class) {
+      return false;
+    }
+    return true;
+  }
+}
diff --git a/python/testData/pyi/inspections/pyiStatementEffect/PyiStatementEffect.pyi b/python/testData/pyi/inspections/pyiStatementEffect/PyiStatementEffect.pyi
new file mode 100644 (file)
index 0000000..80a62af
--- /dev/null
@@ -0,0 +1 @@
+def foo() -> None: ...
diff --git a/python/testData/pyi/inspections/pyiUnusedParameters/PyiUnusedParameters.pyi b/python/testData/pyi/inspections/pyiUnusedParameters/PyiUnusedParameters.pyi
new file mode 100644 (file)
index 0000000..e0a0f03
--- /dev/null
@@ -0,0 +1 @@
+def foo(x: int, y: str) -> str: ...
index 5211e07cf7423a46dbc33026598b97f9b3c678a6..9b465e2c961d2f8160d2d72dee27f8b8d9491c65 100644 (file)
@@ -18,7 +18,9 @@ package com.jetbrains.python.pyi;
 import com.intellij.codeInspection.LocalInspectionTool;
 import com.intellij.psi.PsiDocumentManager;
 import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.inspections.PyStatementEffectInspection;
 import com.jetbrains.python.inspections.PyTypeCheckerInspection;
+import com.jetbrains.python.inspections.PyUnusedLocalInspection;
 import com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection;
 import org.jetbrains.annotations.NotNull;
 
@@ -26,25 +28,41 @@ import org.jetbrains.annotations.NotNull;
  * @author vlan
  */
 public class PyiInspectionsTest extends PyTestCase {
-  private void doTest(@NotNull Class<? extends LocalInspectionTool> inspectionClass) {
+  private void doTest(@NotNull Class<? extends LocalInspectionTool> inspectionClass, @NotNull String extension) {
     myFixture.copyDirectoryToProject("pyi/inspections/" + getTestName(true), "");
     myFixture.copyDirectoryToProject("typing", "");
     PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
-    final String fileName = getTestName(false) + ".py";
+    final String fileName = getTestName(false) + extension;
     myFixture.configureByFile(fileName);
     myFixture.enableInspections(inspectionClass);
     myFixture.checkHighlighting(true, false, true);
   }
 
+  private void doPyTest(@NotNull Class<? extends LocalInspectionTool> inspectionClass) {
+    doTest(inspectionClass, ".py");
+  }
+
+  private void doPyiTest(@NotNull Class<? extends LocalInspectionTool> inspectionClass) {
+    doTest(inspectionClass, ".pyi");
+  }
+
   public void testUnresolvedModuleAttributes() {
-    doTest(PyUnresolvedReferencesInspection.class);
+    doPyTest(PyUnresolvedReferencesInspection.class);
   }
 
   public void testUnresolvedClassAttributes() {
-    doTest(PyUnresolvedReferencesInspection.class);
+    doPyTest(PyUnresolvedReferencesInspection.class);
   }
 
   public void testOverloads() {
-    doTest(PyTypeCheckerInspection.class);
+    doPyTest(PyTypeCheckerInspection.class);
+  }
+
+  public void testPyiUnusedParameters() {
+    doPyiTest(PyUnusedLocalInspection.class);
+  }
+
+  public void testPyiStatementEffect() {
+    doPyiTest(PyStatementEffectInspection.class);
   }
 }