#PY-14614 fixed Add option to ignore PyProtectedMemberInspection in type annotations phpstorm/144.2518
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Wed, 23 Dec 2015 14:57:27 +0000 (17:57 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Thu, 24 Dec 2015 09:12:49 +0000 (12:12 +0300)
python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java
python/testData/inspections/PyProtectedMemberInspection/annotation.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java

index 22a1d1d56fc976c82fdf7bbba0e45b8f9d0c96ef..8ad9527b67846b37d26ad3bcc59780d755f3f3f4 100644 (file)
@@ -24,6 +24,7 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiElementVisitor;
 import com.intellij.psi.PsiReference;
+import com.intellij.psi.util.PsiTreeUtil;
 import com.jetbrains.python.PyBundle;
 import com.jetbrains.python.PyNames;
 import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
@@ -55,6 +56,7 @@ import java.util.List;
  */
 public class PyProtectedMemberInspection extends PyInspection {
   public boolean ignoreTestFunctions = true;
+  public boolean ignoreAnnotations = false;
 
   @Nls
   @NotNull
@@ -90,6 +92,7 @@ public class PyProtectedMemberInspection extends PyInspection {
     @Override
     public void visitPyReferenceExpression(PyReferenceExpression node) {
       final PyExpression qualifier = node.getQualifier();
+      if (ignoreAnnotations && PsiTreeUtil.getParentOfType(node, PyAnnotation.class) != null) return;
       if (qualifier == null || PyNames.CANONICAL_SELF.equals(qualifier.getText())) return;
       checkReference(node, qualifier);
     }
@@ -102,7 +105,6 @@ public class PyProtectedMemberInspection extends PyInspection {
 
       if (name != null && name.startsWith("_") && !name.startsWith("__") && !name.endsWith("__")) {
         final PsiReference reference = node.getReference(getResolveContext());
-        if (reference == null) return;
         for (final PyInspectionExtension inspectionExtension : PyInspectionExtension.EP_NAME.getExtensions()) {
           if (inspectionExtension.ignoreProtectedSymbol(node, myTypeEvalContext)) {
             return;
@@ -161,6 +163,7 @@ public class PyProtectedMemberInspection extends PyInspection {
   public JComponent createOptionsPanel() {
     MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
     panel.addCheckbox("Ignore test functions", "ignoreTestFunctions");
+    panel.addCheckbox("Ignore annotations", "ignoreAnnotations");
     return panel;
   }
 }
diff --git a/python/testData/inspections/PyProtectedMemberInspection/annotation.py b/python/testData/inspections/PyProtectedMemberInspection/annotation.py
new file mode 100644 (file)
index 0000000..7d966cb
--- /dev/null
@@ -0,0 +1,6 @@
+class A:
+  pass
+
+A._inner = 10
+def id(a: A._inner):
+  return a
\ No newline at end of file
index 60221fc49b203ab737e0184f4516155b19edb176..51c81c2474db602a1684834484d6279e80e031e6 100644 (file)
@@ -17,6 +17,7 @@ package com.jetbrains.python.inspections;
 
 import com.intellij.testFramework.TestDataPath;
 import com.jetbrains.python.fixtures.PyTestCase;
+import com.jetbrains.python.psi.LanguageLevel;
 
 /**
  * User: ktisha
@@ -60,6 +61,14 @@ public class PyProtectedMemberInspectionTest extends PyTestCase {
     doTest();
   }
 
+  public void testAnnotation() {
+    setLanguageLevel(LanguageLevel.PYTHON34);
+    PyProtectedMemberInspection inspection = new PyProtectedMemberInspection();
+    inspection.ignoreAnnotations = true;
+    myFixture.configureByFile(getTestName(true) + ".py");
+    myFixture.checkHighlighting(false, false, true);
+  }
+
   public void testModule() {
     myFixture.configureByFiles(getTestName(true) + ".py", "tmp.py");
     myFixture.enableInspections(PyProtectedMemberInspection.class);