fixed PY-11002 "Variable in function should be lowercase" when overriding class

This commit is contained in:
Ekaterina Tuzova
2013-10-03 16:42:42 +04:00
parent f02c7a7686
commit c412235c10
14 changed files with 100 additions and 1 deletions
+1 -1
View File
@@ -326,7 +326,7 @@
<localInspection language="Python" shortName="PyShadowingBuiltinsInspection" displayName="Shadowing built-ins" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyShadowingBuiltinsInspection"/>
<localInspection language="Python" shortName="PyShadowingNamesInspection" displayName="Shadowing names from outer scopes" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyShadowingNamesInspection"/>
<localInspection language="Python" shortName="PyAbstractClassInspection" displayName="Class must implement all abstract methods" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyAbstractClassInspection"/>
<localInspection language="Python" shortName="PyPep8NamingInspection" displayName="PEP 8 naming convention violation" groupKey="INSP.GROUP.python" enabledByDefault="true" level="INFO" implementationClass="com.jetbrains.python.inspections.PyPep8NamingInspection"/>
<localInspection language="Python" shortName="PyPep8NamingInspection" displayName="PEP 8 naming convention violation" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyPep8NamingInspection"/>
<liveTemplateContext implementation="com.jetbrains.python.codeInsight.liveTemplates.PythonTemplateContextType"/>
<liveTemplateMacro implementation="com.jetbrains.python.codeInsight.liveTemplates.CollectionElementNameMacro"/>
@@ -10,6 +10,9 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.jetbrains.python.psi.search.PySuperMethodsSearch;
import com.jetbrains.python.psi.types.PyModuleType;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.TypeEvalContext;
import com.jetbrains.python.testing.pytest.PyTestUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -48,6 +51,13 @@ public class PyPep8NamingInspection extends PyInspection {
for (PyExpression expression : node.getTargets()) {
final String name = expression.getName();
if (name == null) continue;
if (expression instanceof PyTargetExpression) {
final PyExpression qualifier = ((PyTargetExpression)expression).getQualifier();
if (qualifier != null) {
final PyType type = TypeEvalContext.codeAnalysis(node.getContainingFile()).getType(qualifier);
if (type instanceof PyModuleType) return;
}
}
if (!LOWERCASE_REGEX.matcher(name).matches() && !name.startsWith("_")) {
registerProblem(expression, "Variable in function should be lowercase", new PyRenameElementQuickFix());
}
@@ -0,0 +1 @@
def do_stuff(a, <weak_warning descr="Argument name should be lowercase">BCD</weak_warning>): pass
@@ -0,0 +1,4 @@
class <weak_warning descr="Class names should use CamelCase convention">A_B</weak_warning>:
def foo(self):
pass
@@ -0,0 +1 @@
def <weak_warning descr="Function name should be lowercase">doStuff</weak_warning>(a): print a
@@ -0,0 +1,5 @@
def do_stuff(): pass
class A:
def foo(self):
<weak_warning descr="Variable in function should be lowercase">Abc</weak_warning> = do_stuff()
@@ -0,0 +1 @@
from x import TestX as <weak_warning descr="CamelCase variable imported as lowercase">test</weak_warning>
@@ -0,0 +1 @@
from x import TEST as <weak_warning descr="Constant variable imported as non constant">test</weak_warning>
@@ -0,0 +1 @@
from x import y as <weak_warning descr="Lowercase variable imported as non lowercase">TEST</weak_warning>
@@ -0,0 +1,5 @@
class A:
def <weak_warning descr="Function name should be lowercase">fooBar</weak_warning>(self): pass
class B(A):
def fooBar(self): pass
@@ -0,0 +1,5 @@
import tmp1
class A:
def set_up(self):
tmp1.X = do_stuff()
@@ -0,0 +1,5 @@
from unittest import TestCase
class TestX(TestCase):
def testFoo(self):
self.assertTrue(True)
@@ -0,0 +1,2 @@
class X:
pass
@@ -0,0 +1,58 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
/**
* User: ktisha
*/
public class PyPep8NamingInspectionTest extends PyTestCase {
public void testFunctionVariable() {
doTest();
}
public void testClassName() {
doTest();
}
public void testArgumentName() {
doTest();
}
public void testFunctionName() {
doTest();
}
public void testImportConstant() {
doTest();
}
public void testImportCamelAsLower() {
doTest();
}
public void testImportLowerAsNonLower() {
doTest();
}
public void testOverridden() {
doTest();
}
public void testTest() {
doTest();
}
public void testOverrideFromModule() {
myFixture.configureByFiles("inspections/PyPep8NamingInspection/" + getTestName(true) + ".py",
"inspections/PyPep8NamingInspection/tmp1.py");
myFixture.enableInspections(PyPep8NamingInspection.class);
myFixture.checkHighlighting(false, false, true);
}
private void doTest() {
myFixture.configureByFile("inspections/PyPep8NamingInspection/" + getTestName(true) + ".py");
myFixture.enableInspections(PyPep8NamingInspection.class);
myFixture.checkHighlighting(false, false, true);
}
}