From 1fae7b975401ee258ebaeb91d9296c1b8de53d73 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 27 Aug 2012 16:59:40 +0200 Subject: [PATCH] don't highlight first parameter of staticmethods in metaclasses (PY-6648) --- .../PyMethodParametersInspection.java | 23 +++--- .../PyMethodParametersInspection/expected.xml | 58 -------------- .../PyMethodParametersInspection/test.py | 80 +++++++++++++++++++ .../python/PythonInspectionsTest.java | 3 +- 4 files changed, 94 insertions(+), 70 deletions(-) delete mode 100644 python/testData/inspections/PyMethodParametersInspection/expected.xml create mode 100644 python/testData/inspections/PyMethodParametersInspection/test.py diff --git a/python/src/com/jetbrains/python/inspections/PyMethodParametersInspection.java b/python/src/com/jetbrains/python/inspections/PyMethodParametersInspection.java index a09b7a4d8507..54ea4dc26c67 100644 --- a/python/src/com/jetbrains/python/inspections/PyMethodParametersInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyMethodParametersInspection.java @@ -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(), diff --git a/python/testData/inspections/PyMethodParametersInspection/expected.xml b/python/testData/inspections/PyMethodParametersInspection/expected.xml deleted file mode 100644 index 6eb21015da29..000000000000 --- a/python/testData/inspections/PyMethodParametersInspection/expected.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - first_arg.py - 13 - Usually first parameter of a method is named 'self' - - - first_arg.py - 16 - Method must have a first parameter, usually called 'self' - - - first_arg.py - 19 - First parameter of a non-static method must not be a tuple - - - first_arg.py - 25 - Usually first parameter of such methods is named 'cls' - - - first_arg.py - 29 - Usually first parameter of such methods is named 'cls' - - - first_arg.py - 34 - Usually first parameter of such methods is named 'cls' - - - first_arg.py - 39 - Usually first parameter of such methods is named 'cls' - - - first_arg.py - 62 - Usually first parameter of such methods is named 'self' - - - first_arg.py - 65 - Usually first parameter of such methods is named 'mcs' - - - first_arg.py - 68 - Usually first parameter of such methods is named 'cls' - - - first_arg.py - 75 - Usually first parameter of such methods is named 'mcs' - - diff --git a/python/testData/inspections/PyMethodParametersInspection/test.py b/python/testData/inspections/PyMethodParametersInspection/test.py new file mode 100644 index 000000000000..e03b8e9bd366 --- /dev/null +++ b/python/testData/inspections/PyMethodParametersInspection/test.py @@ -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__(a, b): + pass # rename a + + def foo(): + pass # propose self + + def loo((l, g), *rest): + pass # complain at tuple + + def zoo(*animals): + pass # nothing + + def __new__(self): + pass # propose cls + + @classmethod + def boo(self): + pass # propose cls + + @classmethod + @innocent + def boo(self): + pass # propose cls + + @innocent + @classmethod + def boo(self): + 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(first): # rename to "self" + pass + + def __new__(self, *rest): # rename to "mcs" + pass + + def __call__(self): # rename to "cls" + pass + + def bar(cls): # <- rename to "self" + return "foobar" + + @classmethod + def baz(moo): # <- rename to "mcs" + return "foobar" + + @staticmethod + def bazz(param1): + return "foobar" diff --git a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java index 8f63f47a57ab..2952720da43d 100644 --- a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java +++ b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java @@ -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() {