Skip to content

Commit beca529

Browse files
joke1196sonartech
authored andcommitted
SONARPY-3945 Fix QG and migration to type V2 for NumpyListOverGeneratorCheck (#980)
GitOrigin-RevId: 232cb45f4f2380b3dd6d0f9f59c3e0fc45a73472
1 parent 64d954d commit beca529

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,34 @@
2222
import org.sonar.check.Rule;
2323
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
2424
import org.sonar.plugins.python.api.SubscriptionContext;
25-
import org.sonar.plugins.python.api.symbols.Symbol;
2625
import org.sonar.plugins.python.api.tree.Argument;
2726
import org.sonar.plugins.python.api.tree.CallExpression;
2827
import org.sonar.plugins.python.api.tree.Expression;
29-
import org.sonar.plugins.python.api.tree.HasSymbol;
3028
import org.sonar.plugins.python.api.tree.Name;
3129
import org.sonar.plugins.python.api.tree.RegularArgument;
3230
import org.sonar.plugins.python.api.tree.Tree;
31+
import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher;
32+
import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers;
3333
import org.sonar.python.tree.TreeUtils;
3434

3535
@Rule(key = "S6714")
3636
public class NumpyListOverGeneratorCheck extends PythonSubscriptionCheck {
3737

3838
public static final String MESSAGE = "Pass a list to \"np.array\" instead of passing a generator.";
3939

40+
private static final TypeMatcher IS_NUMPY_ARRAY = TypeMatchers.withFQN("numpy.array");
41+
private static final TypeMatcher IS_OBJECT_TYPE = TypeMatchers.isType("builtins.object");
42+
4043
@Override
4144
public void initialize(Context context) {
42-
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::checkNumpyArrayCall);
45+
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, NumpyListOverGeneratorCheck::checkNumpyArrayCall);
4346
}
4447

45-
private void checkNumpyArrayCall(SubscriptionContext ctx) {
48+
private static void checkNumpyArrayCall(SubscriptionContext ctx) {
4649
CallExpression call = (CallExpression) ctx.syntaxNode();
47-
Optional.ofNullable(call.calleeSymbol())
48-
.map(Symbol::fullyQualifiedName)
49-
.filter("numpy.array"::equals)
50-
.ifPresent(fqn -> checkGeneratorCallee(call, ctx));
50+
if (IS_NUMPY_ARRAY.isTrueFor(call.callee(), ctx)) {
51+
checkGeneratorCallee(call, ctx);
52+
}
5153
}
5254

5355
private static void checkGeneratorCallee(CallExpression call, SubscriptionContext ctx) {
@@ -65,13 +67,8 @@ private static void checkGeneratorCallee(CallExpression call, SubscriptionContex
6567
return;
6668
}
6769

68-
if (Optional.ofNullable(TreeUtils.nthArgumentOrKeyword(1, "dtype", argList))
69-
.filter(regArg -> regArg.expression().is(Tree.Kind.NAME))
70-
.map(regArg -> (Name) regArg.expression())
71-
.map(HasSymbol::symbol)
72-
.map(Symbol::fullyQualifiedName)
73-
.filter("object"::equals)
74-
.isEmpty()) {
70+
RegularArgument dtypeArg = TreeUtils.nthArgumentOrKeyword(1, "dtype", argList);
71+
if (dtypeArg == null || !IS_OBJECT_TYPE.isTrueFor(dtypeArg.expression(), ctx)) {
7572
ctx.addIssue(call, MESSAGE);
7673
}
7774
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public class PytzTimeZoneInDatetimeConstructorCheck extends PythonSubscriptionCh
3737

3838
@Override
3939
public void initialize(Context context) {
40-
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, this::checkCallExpression);
40+
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, PytzTimeZoneInDatetimeConstructorCheck::checkCallExpression);
4141
}
4242

43-
private void checkCallExpression(SubscriptionContext context) {
43+
private static void checkCallExpression(SubscriptionContext context) {
4444
CallExpression callExpression = (CallExpression) context.syntaxNode();
4545
Symbol calleeSymbol = callExpression.calleeSymbol();
4646

0 commit comments

Comments
 (0)