don't highlight first parameter of staticmethods in metaclasses (PY-6648)

This commit is contained in:
Dmitry Jemerov
2012-08-27 17:11:56 +02:00
parent 90c31fcf10
commit 1fae7b9754
4 changed files with 94 additions and 70 deletions
@@ -83,7 +83,7 @@ public class PyMethodParametersInspection extends PyInspection {
if (flags != null) {
PyParameterList plist = node.getParameterList();
PyParameter[] params = plist.getParameters();
final String method_name = node.getName();
final String methodName = node.getName();
final String CLS = "cls"; // TODO: move to style settings
final String MCS = "mcs"; // as per pylint inspection C0203
if (params.length == 0) { // fix: add
@@ -138,27 +138,30 @@ public class PyMethodParametersInspection extends PyInspection {
return;
}
if (flags.isMetaclassMethod()) {
String expected_name;
if (flags.isStaticMethod() && !PyNames.NEW.equals(methodName)) {
return;
}
String expectedName;
String alternativeName = null;
if (PyNames.NEW.equals(method_name) || flags.isClassMethod()) {
expected_name = MCS;
if (PyNames.NEW.equals(methodName) || flags.isClassMethod()) {
expectedName = MCS;
}
else if (flags.isSpecialMetaclassMethod()) {
expected_name = CLS;
expectedName = CLS;
}
else {
expected_name = PyNames.CANONICAL_SELF;
expectedName = PyNames.CANONICAL_SELF;
alternativeName = CLS;
}
if (!expected_name.equals(pname) && (alternativeName == null || !alternativeName.equals(pname))) {
if (!expectedName.equals(pname) && (alternativeName == null || !alternativeName.equals(pname))) {
registerProblem(
PyUtil.sure(params[0].getNode()).getPsi(),
PyBundle.message("INSP.usually.named.$0", expected_name),
new RenameParameterQuickFix(expected_name)
PyBundle.message("INSP.usually.named.$0", expectedName),
new RenameParameterQuickFix(expectedName)
);
}
}
else if (flags.isClassMethod() || PyNames.NEW.equals(method_name)) {
else if (flags.isClassMethod() || PyNames.NEW.equals(methodName)) {
if (!CLS.equals(pname)) {
registerProblem(
PyUtil.sure(params[0].getNode()).getPsi(),
@@ -1,58 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>first_arg.py</file>
<line>13</line>
<description>Usually first parameter of a method is named 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>16</line>
<description>Method must have a first parameter, usually called 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>19</line>
<description>First parameter of a non-static method must not be a tuple</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>25</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>29</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>34</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>39</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>62</line>
<description>Usually first parameter of such methods is named 'self'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>65</line>
<description>Usually first parameter of such methods is named 'mcs'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>68</line>
<description>Usually first parameter of such methods is named 'cls'</description>
</problem>
<problem>
<file>first_arg.py</file>
<line>75</line>
<description>Usually first parameter of such methods is named 'mcs'</description>
</problem>
</problems>
@@ -0,0 +1,80 @@
def foo(bar):
pass # nothing
def __call__(foo):
pass # nothing, out of class
def innocent(f):
"innocent deco"
return f
class Foo(object):
def __init__(<weak_warning descr="Usually first parameter of a method is named 'self'">a</weak_warning>, b):
pass # rename a
def foo<error descr="Method must have a first parameter, usually called 'self'">()</error>:
pass # propose self
def loo<weak_warning descr="First parameter of a non-static method must not be a tuple">((l, g), *rest)</weak_warning>:
pass # complain at tuple
def zoo(*animals):
pass # nothing
def __new__(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@classmethod
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@classmethod
@innocent
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@innocent
@classmethod
def boo(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>):
pass # propose cls
@staticmethod
def moo(a):
pass # nothing
@staticmethod
def qoo((x, y, z), t):
pass # nothing
@staticmethod
@innocent
def qoo((x, y, z), t):
pass # nothing
@innocent
@staticmethod
def qoo((x, y, z), t):
pass # nothing
class Meta(type):
def foo(<weak_warning descr="Usually first parameter of such methods is named 'self'">first</weak_warning>): # rename to "self"
pass
def __new__(<weak_warning descr="Usually first parameter of such methods is named 'mcs'">self</weak_warning>, *rest): # rename to "mcs"
pass
def __call__(<weak_warning descr="Usually first parameter of such methods is named 'cls'">self</weak_warning>): # rename to "cls"
pass
def bar(cls): # <- rename to "self"
return "foobar"
@classmethod
def baz(<weak_warning descr="Usually first parameter of such methods is named 'mcs'">moo</weak_warning>): # <- rename to "mcs"
return "foobar"
@staticmethod
def bazz(param1):
return "foobar"
@@ -49,8 +49,7 @@ public class PythonInspectionsTest extends PyTestCase {
}
public void testPyMethodParametersInspection() {
LocalInspectionTool inspection = new PyMethodParametersInspection();
doTest(getTestName(false), inspection);
doHighlightingTest(PyMethodParametersInspection.class);
}
public void testPyNestedDecoratorsInspection() {