Skip to content

Commit f21a39d

Browse files
sonar-nigel[bot]Vibe Bot
authored andcommitted
SONARPY-3563 Fix S100 false positives on unittest/Django test lifecycle methods (#988)
Co-authored-by: Vibe Bot <vibe-bot@sonarsource.com> GitOrigin-RevId: 011d31e86ca30961048c71bad5676aaf336c1c1b
1 parent b31a132 commit f21a39d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

python-checks/src/main/java/org/sonar/python/checks/MethodNameCheck.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.sonar.python.checks;
1818

19+
import java.util.Set;
1920
import org.sonar.check.Rule;
2021
import org.sonar.plugins.python.api.tree.FunctionDef;
2122

@@ -26,13 +27,17 @@
2627
public class MethodNameCheck extends AbstractFunctionNameCheck {
2728
public static final String CHECK_KEY = "S100";
2829

30+
private static final Set<String> WHITELIST = Set.of("setUp", "tearDown", "setUpClass", "tearDownClass", "setUpTestData");
31+
2932
@Override
3033
public String typeName() {
3134
return "method";
3235
}
3336

3437
@Override
3538
public boolean shouldCheckFunctionDeclaration(FunctionDef pyFunctionDefTree) {
36-
return pyFunctionDefTree.isMethodDefinition() && !classHasInheritance(getParentClassDef(pyFunctionDefTree));
39+
return pyFunctionDefTree.isMethodDefinition()
40+
&& !classHasInheritance(getParentClassDef(pyFunctionDefTree))
41+
&& !WHITELIST.contains(pyFunctionDefTree.name().name());
3742
}
3843
}

python-checks/src/main/java/org/sonar/python/checks/utils/CheckUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ public static ClassDef getParentClassDef(Tree tree) {
105105
return null;
106106
}
107107

108-
public static boolean classHasInheritance(ClassDef classDef) {
108+
public static boolean classHasInheritance(@Nullable ClassDef classDef) {
109+
if (classDef == null) {
110+
return false;
111+
}
109112
ArgList argList = classDef.args();
110113
if (argList == null) {
111114
return false;

python-checks/src/test/resources/checks/methodName.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,41 @@ def y(self): # OK
5656

5757
def _v(self):
5858
...
59+
60+
class TestMixin:
61+
@classmethod
62+
def setUpTestData(cls):
63+
pass
64+
65+
def setUp(self):
66+
pass
67+
68+
def tearDown(self):
69+
pass
70+
71+
@classmethod
72+
def setUpClass(cls):
73+
pass
74+
75+
@classmethod
76+
def tearDownClass(cls):
77+
pass
78+
79+
class TestMixinWithObjectBase(object):
80+
def setUp(self): # OK, whitelisted lifecycle method even in class inheriting from object
81+
pass
82+
83+
def tearDown(self): # OK, whitelisted lifecycle method
84+
pass
85+
86+
@classmethod
87+
def setUpClass(cls): # OK, whitelisted lifecycle method
88+
pass
89+
90+
@classmethod
91+
def tearDownClass(cls): # OK, whitelisted lifecycle method
92+
pass
93+
94+
@classmethod
95+
def setUpTestData(cls): # OK, whitelisted lifecycle method
96+
pass

0 commit comments

Comments
 (0)