diff --git a/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java b/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java index cf4fc7976ce2..18894d5e12a4 100644 --- a/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java @@ -17,6 +17,7 @@ package com.jetbrains.python.inspections; import com.intellij.codeInspection.LocalInspectionToolSession; import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiReference; @@ -29,10 +30,13 @@ import com.jetbrains.python.psi.PyExpression; import com.jetbrains.python.psi.PyReferenceExpression; import com.jetbrains.python.psi.types.PyModuleType; import com.jetbrains.python.psi.types.PyType; +import com.jetbrains.python.testing.pytest.PyTestUtil; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.swing.*; + /** * User: ktisha * @@ -41,6 +45,8 @@ import org.jetbrains.annotations.Nullable; * is access outside the class or a descendant of the class where it's defined. */ public class PyProtectedMemberInspection extends PyInspection { + public boolean ignoreTestFunctions = true; + @Nls @NotNull @Override @@ -57,7 +63,7 @@ public class PyProtectedMemberInspection extends PyInspection { } - private static class Visitor extends PyInspectionVisitor { + private class Visitor extends PyInspectionVisitor { public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) { super(holder, session); } @@ -70,6 +76,7 @@ public class PyProtectedMemberInspection extends PyInspection { if (name != null && name.startsWith("_") && !name.startsWith("__") && !name.endsWith("__")) { final PyClass parentClass = getClassOwner(node); if (parentClass != null) { + if (PyTestUtil.isPyTestClass(parentClass) && ignoreTestFunctions) return; final PsiReference reference = node.getReference(getResolveContext()); if (reference != null) { final PsiElement resolvedExpression = reference.resolve(); @@ -95,7 +102,7 @@ public class PyProtectedMemberInspection extends PyInspection { } @Nullable - private static PyClass getClassOwner(@Nullable PsiElement element) { + private PyClass getClassOwner(@Nullable PsiElement element) { for (ScopeOwner owner = ScopeUtil.getScopeOwner(element); owner != null; owner = ScopeUtil.getScopeOwner(owner)) { if (owner instanceof PyClass) { return (PyClass)owner; @@ -104,4 +111,12 @@ public class PyProtectedMemberInspection extends PyInspection { return null; } } + + @Nullable + @Override + public JComponent createOptionsPanel() { + MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox("Ignore test functions", "ignoreTestFunctions"); + return panel; + } } diff --git a/python/testData/inspections/PyProtectedMemberInspection/test.py b/python/testData/inspections/PyProtectedMemberInspection/test.py new file mode 100644 index 000000000000..d8f6cdc66947 --- /dev/null +++ b/python/testData/inspections/PyProtectedMemberInspection/test.py @@ -0,0 +1,17 @@ +__author__ = 'ktisha' + +import unittest + +class A: + def __init__(self): + self._x = 1 + + def _foo(self): + pass + + +class TestA(unittest.TestCase): + def testA(self): + a = A() + a._foo() + print(a._x) diff --git a/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java index 36caa8905b18..3daa7e36e311 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java @@ -46,6 +46,10 @@ public class PyProtectedMemberInspectionTest extends PyTestCase { doTest(); } + public void testTest() { + doTest(); + } + public void testModule() { myFixture.configureByFiles(getTestName(true) + ".py", "tmp.py"); myFixture.enableInspections(PyProtectedMemberInspection.class);