mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
implemented W0212 pylint inspection
Access to a protected member %s of a client class
This commit is contained in:
@@ -317,6 +317,7 @@
|
||||
<localInspection language="Python" shortName="PyPackageRequirementsInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.requirements" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyPackageRequirementsInspection"/>
|
||||
<localInspection language="Python" shortName="PyPep8Inspection" displayName="PEP 8 coding style violation" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyPep8Inspection"/>
|
||||
<localInspection language="Python" shortName="PyAttributeOutsideInitInspection" displayName="Instance attribute defined outside _init_" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyAttributeOutsideInitInspection"/>
|
||||
<localInspection language="Python" shortName="PyProtectedMemberInspection" displayName="Access to a protected member of a class" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyProtectedMemberInspection"/>
|
||||
<localInspection language="Python" shortName="PyDocstringTypesInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.docstring.types" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyDocstringTypesInspection"/>
|
||||
<localInspection language="Python" shortName="PyShadowingBuiltinsInspection" displayName="Shadowing built-ins" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyShadowingBuiltinsInspection"/>
|
||||
|
||||
|
||||
@@ -435,6 +435,10 @@ INSP.NAME.chained.comparisons=Chained comparisons can be simplified
|
||||
INSP.NAME.attribute.outside.init=Instance attribute defined outside _init_
|
||||
INSP.attribute.$0.outside.init=Instance attribute {0} defined outside _init_
|
||||
|
||||
# PyProtectedMemberInspection
|
||||
INSP.NAME.protected.member.access=Access to a protected member of a class
|
||||
INSP.protected.member.$0.access=Access to a protected member {0} of a class
|
||||
|
||||
# PyArgumentEqualDefaultInspection
|
||||
INSP.NAME.argument.equal.default=Argument passed to function is equal to default parameter value
|
||||
INSP.argument.equals.to.default=Argument equals to default parameter value
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.jetbrains.python.inspections;
|
||||
|
||||
import com.intellij.codeInspection.LocalInspectionToolSession;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
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.psi.PyClass;
|
||||
import com.jetbrains.python.psi.PyExpression;
|
||||
import com.jetbrains.python.psi.PyReferenceExpression;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*
|
||||
* Inspection to detect situations, where
|
||||
* protected member (i.e. class member with a name beginning with an underscore)
|
||||
* is access outside the class or a descendant of the class where it's defined.
|
||||
*/
|
||||
public class PyProtectedMemberInspection extends PyInspection {
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return PyBundle.message("INSP.NAME.protected.member.access");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
|
||||
boolean isOnTheFly,
|
||||
@NotNull LocalInspectionToolSession session) {
|
||||
return new Visitor(holder, session);
|
||||
}
|
||||
|
||||
|
||||
private static class Visitor extends PyInspectionVisitor {
|
||||
public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
|
||||
super(holder, session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyReferenceExpression(PyReferenceExpression node) {
|
||||
final PyExpression qualifier = node.getQualifier();
|
||||
if (qualifier == null) return;
|
||||
final String name = node.getName();
|
||||
if (name != null && name.startsWith("_")) {
|
||||
final PyClass parentClass = PsiTreeUtil.getParentOfType(node, PyClass.class);
|
||||
if (parentClass != null) {
|
||||
final PsiReference reference = node.getReference();
|
||||
final PsiElement resolvedExpression = reference.resolve();
|
||||
final PyClass resolvedClass = PsiTreeUtil.getParentOfType(resolvedExpression, PyClass.class);
|
||||
if (parentClass.isSubclass(resolvedClass))
|
||||
return;
|
||||
}
|
||||
registerProblem(node, PyBundle.message("INSP.protected.member.$0.access", name));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class A:
|
||||
def __init__(self):
|
||||
self._a = 1
|
||||
|
||||
def foo(self):
|
||||
self.b= 1
|
||||
|
||||
class B(A):
|
||||
def __init__(self):
|
||||
A.__init__(self)
|
||||
self.b = self._a
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class A:
|
||||
def __init__(self):
|
||||
self._a = 1
|
||||
|
||||
def foo(self):
|
||||
self.b= 1
|
||||
|
||||
|
||||
print <weak_warning descr="Access to a protected member _a of a class">A()._a</weak_warning>
|
||||
@@ -0,0 +1,12 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class A:
|
||||
def __init__(self):
|
||||
self._a = 1
|
||||
|
||||
def foo(self):
|
||||
self.b= 1
|
||||
|
||||
class B:
|
||||
def __init__(self):
|
||||
<weak_warning descr="Access to a protected member _a of a class">A()._a</weak_warning>
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.jetbrains.python.inspections;
|
||||
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyProtectedMemberInspectionTest extends PyTestCase {
|
||||
|
||||
public void testTruePositive() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testTruePositiveInClass() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testTrueNegative() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile("inspections/PyProtectedMemberInspection/" + getTestName(true) + ".py");
|
||||
myFixture.enableInspections(PyProtectedMemberInspection.class);
|
||||
myFixture.checkHighlighting(false, false, true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user