From 934f3da2a54039eec5aeff5bd04f126df8ee3337 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Sat, 1 Jul 2017 17:35:15 +0300 Subject: [PATCH] PY-24817 Fixed: False positive: Method may be 'static' when docstring is present Update PyMethodMayBeStaticInspection to consider more methods as empty ones. --- .../python/inspections/PyMethodMayBeStaticInspection.java | 7 ++----- .../PyMethodMayBeStaticInspection/documentedEmpty.py | 4 ++++ .../inspections/PyMethodMayBeStaticInspectionTest.java | 7 ++++++- 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 python/testData/inspections/PyMethodMayBeStaticInspection/documentedEmpty.py diff --git a/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java b/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java index f3f81ad6473e..24a3c1f4cf51 100644 --- a/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyMethodMayBeStaticInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,10 +78,7 @@ public class PyMethodMayBeStaticInspection extends PyInspection { if (!attributes.isEmpty()) return; if (isTestElement(node)) return; - final PyStatementList statementList = node.getStatementList(); - final PyStatement[] statements = statementList.getStatements(); - - if (statements.length == 1 && statements[0] instanceof PyPassStatement) return; + if (PyUtil.isEmptyFunction(node)) return; final PyParameter[] parameters = node.getParameterList().getParameters(); diff --git a/python/testData/inspections/PyMethodMayBeStaticInspection/documentedEmpty.py b/python/testData/inspections/PyMethodMayBeStaticInspection/documentedEmpty.py new file mode 100644 index 000000000000..55f88475538c --- /dev/null +++ b/python/testData/inspections/PyMethodMayBeStaticInspection/documentedEmpty.py @@ -0,0 +1,4 @@ +class A: + def foo(self): + """Do something""" + pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java index 1d33844e87e2..7a84199d2127 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyMethodMayBeStaticInspectionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,6 +105,11 @@ public class PyMethodMayBeStaticInspectionTest extends PyTestCase { runWithLanguageLevel(LanguageLevel.PYTHON30, () -> doTest()); } + // PY-24817 + public void testDocumentedEmpty() { + doTest(); + } + private void doTest() { myFixture.configureByFile(getTestName(true) + ".py"); myFixture.enableInspections(PyMethodMayBeStaticInspection.class);