implemented pylint W0601

Inspection is used when a variable is defined through the "global" statement but the variable is not defined in the module scope.
This commit is contained in:
Ekaterina Tuzova
2013-03-22 15:44:16 +04:00
parent d74c7f2ab1
commit d7281a83a9
10 changed files with 99 additions and 6 deletions
@@ -319,6 +319,7 @@
<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="PyClassHasNoInitInspection" displayName="Class has no __init__ method" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyClassHasNoInitInspection"/>
<localInspection language="Python" shortName="PyNoneFunctionAssignmentInspection" displayName="Assigning function call that doesn't return anything" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyNoneFunctionAssignmentInspection"/>
<localInspection language="Python" shortName="PyGlobalUndefinedInspection" displayName="Global variable is undefined at the module level" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyGlobalUndefinedInspection"/>
<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"/>
@@ -474,11 +474,15 @@ INSP.NAME.requirements=Package requirements
# PyClassHasNoInitInspection
INSP.NAME.class.has.no.init=Class has no __init__ method
INSP.class.has.no.init=Class has no __init__ method
INSP.parent.$0.has.no.init=Parent {0} has no __init__ method
INSP.parent.$0.has.no.init=Parent ''{0}'' has no __init__ method
#PyNoneFunctionAssignmentInspection
INSP.NAME.none.function.assignment=Assigning function call that doesn't return anything (None)
INSP.none.function.assignment=Function {0} doesn''t return anything
INSP.none.function.assignment=Function ''{0}'' doesn''t return anything
#PyGlobalUndefinedInspection
INSP.NAME.global.undefined=Global variable is undefined at the module level
INSP.NAME.global.$0.undefined=Global variable ''{0}'' is undefined at the module level
# Refactoring
# introduce
@@ -0,0 +1,51 @@
package com.jetbrains.python.inspections;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyGlobalStatement;
import com.jetbrains.python.psi.PyTargetExpression;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: ktisha
*
* pylint W0601
*/
public class PyGlobalUndefinedInspection extends PyInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return PyBundle.message("INSP.NAME.global.undefined");
}
@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 visitPyGlobalStatement(PyGlobalStatement node) {
final PyTargetExpression[] globals = node.getGlobals();
for (PyTargetExpression global : globals) {
if (global.getReference().resolve() == global) {
registerProblem(global, PyBundle.message("INSP.NAME.global.$0.undefined", global.getName()));
}
}
}
}
}
@@ -3,7 +3,7 @@ class <weak_warning descr="Class has no __init__ method">A</weak_warning>:
def foo(self):
self.b = 1
class <weak_warning descr="Parent A has no __init__ method">B</weak_warning>(A):
class <weak_warning descr="Parent 'A' has no __init__ method">B</weak_warning>(A):
def __init__(self):
self.b = 2
@@ -0,0 +1,8 @@
__author__ = 'ktisha'
bar = 1
def foo():
global bar
print bar
foo()
@@ -0,0 +1,6 @@
__author__ = 'ktisha'
def foo():
global <weak_warning descr="Global variable 'bar' is undefined at the module level">bar</weak_warning>
print bar
foo()
@@ -3,4 +3,4 @@ __author__ = 'ktisha'
def foo():
a = 1
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
<weak_warning descr="Function 'foo' doesn't return anything">b = foo()</weak_warning>
@@ -3,4 +3,4 @@ __author__ = 'ktisha'
def foo():
pass
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
<weak_warning descr="Function 'foo' doesn't return anything">b = foo()</weak_warning>
@@ -3,4 +3,4 @@ __author__ = 'ktisha'
def foo():
return None
<weak_warning descr="Function foo doesn't return anything">b = foo()</weak_warning>
<weak_warning descr="Function 'foo' doesn't return anything">b = foo()</weak_warning>
@@ -0,0 +1,23 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
/**
* User: ktisha
*/
public class PyGlobalUndefinedInspectionTest extends PyTestCase {
public void testTruePositive() {
doTest();
}
public void testTrueNegative() {
doTest();
}
private void doTest() {
myFixture.configureByFile("inspections/PyGlobalUndefinedInspection/" + getTestName(true) + ".py");
myFixture.enableInspections(PyGlobalUndefinedInspection.class);
myFixture.checkHighlighting(false, false, true);
}
}