Skip to content

Commit 9ae386e

Browse files
authored
Merge branch 'main' into GCI100-python
2 parents 33a25e4 + f44d097 commit 9ae386e

File tree

14 files changed

+439
-3
lines changed

14 files changed

+439
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- [#73](https://github.com/green-code-initiative/creedengo-python/pull/73) Add rule GCI100 Disable Gradient For model eval, a rule specific to PyTorch and AI/ML
13+
- [#77](https://github.com/green-code-initiative/creedengo-python/pull/77) Add rule GCI104 AvoidCreatingTensorUsingNumpyOrNativePython, a rule specific to AI/ML code
14+
- [#70](https://github.com/green-code-initiative/creedengo-python/pull/70) Add rule GCI108 Prefer Append Left (a rule to prefer the use of `append` over `insert` for list, using deques)
1315
- [#78](https://github.com/green-code-initiative/creedengo-python/pull/78) Add rule GCI105 on String Concatenation. This rule may also apply to other rules
1416
- [#74](https://github.com/green-code-initiative/creedengo-python/pull/74) Add rule GCI101 Avoid Conv Bias Before Batch Normalization, a rule specific to Deeplearning
1517
- [#75](https://github.com/green-code-initiative/creedengo-python/pull/75) Add rule GCI102 avoid non pinned memory for dataloader. This rule is specific to PyTorch and so AI

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
<!-- NOT OK : version with compatibility problem : classes not found -->
6666
<!-- <sonarpython.version>4.22.0.16914</sonarpython.version>-->
6767
<!-- <sonarpython.version>5.4.0.22255</sonarpython.version>-->
68+
<!-- <sonarpython.version>5.8.0.24785</sonarpython.version>-->
6869

69-
<mockito.version>5.17.0</mockito.version>
7070
<lombok.version>1.18.38</lombok.version>
7171

7272
<!-- temporary version waiting for a real automatic release in creedengo repository -->
@@ -137,7 +137,7 @@
137137
<dependency>
138138
<groupId>org.mockito</groupId>
139139
<artifactId>mockito-junit-jupiter</artifactId>
140-
<version>${mockito.version}</version>
140+
<version>5.19.0</version>
141141
<scope>test</scope>
142142
</dependency>
143143

src/it/java/org/greencodeinitiative/creedengo/python/integration/tests/GCIRulesIT.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ void testGCI103(){
382382

383383
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
384384
}
385+
386+
@Test
387+
void testGCI104() {
388+
389+
String filePath = "src/avoidCreatingTensorUsingNumpyOrNativePython.py";
390+
String ruleId = "creedengo-python:GCI104";
391+
String ruleMsg = "Directly create tensors as torch.Tensor instead of using numpy functions.";
392+
int[] startLines = new int[]{
393+
5, 15, 19, 24
394+
};
395+
int[] endLines = new int[]{
396+
5, 15, 19, 24
397+
};
398+
399+
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
400+
}
385401

386402
@Test
387403
void testGCI105() {
@@ -430,4 +446,19 @@ void testGCI107(){
430446
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
431447
}
432448

449+
@Test
450+
void testGCI108(){
451+
String filePath = "src/preferAppendLeft.py";
452+
String ruleId = "creedengo-python:GCI108";
453+
String ruleMsg = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
454+
int[] startLines = new int[]{
455+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
456+
};
457+
int[] endLines = new int[]{
458+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
459+
};
460+
461+
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
462+
}
463+
433464
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import torch
3+
4+
def non_compliant_random_rand():
5+
tensor = torch.tensor(np.random.rand(1000, 1000)) # Noncompliant {{Directly create tensors as torch.Tensor instead of using numpy functions.}}
6+
7+
def compliant_random_rand():
8+
tensor = torch.rand([1000, 1000])
9+
10+
def compliant_zeros():
11+
tensor_ = torch.zeros(1, 2)
12+
print(tensor_)
13+
14+
def non_compliant_zeros():
15+
tensor_ = torch.IntTensor(np.zeros(1, 2)) # Noncompliant {{Directly create tensors as torch.Tensor instead of using numpy functions.}}
16+
print(tensor_)
17+
18+
def non_compliant_eye():
19+
tensor = torch.cuda.LongTensor(np.eye(5)) # Noncompliant {{Directly create tensors as torch.Tensor instead of using numpy functions.}}
20+
21+
def non_compliant_ones():
22+
import numpy
23+
from torch import FloatTensor
24+
tensor = FloatTensor(data=np.ones(shape=(1, 5))) # Noncompliant {{Directly create tensors as torch.Tensor instead of using numpy functions.}}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import deque
2+
3+
# Cas non conformes
4+
numbers = []
5+
numbers.insert(0, val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
6+
7+
items = []
8+
items.insert(0, "start") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
9+
10+
lst = []
11+
(lst).insert(0, 'x') # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
12+
13+
x = []
14+
(x).insert(0, value) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
15+
16+
def insert_first(l, v):
17+
l.insert(0, v) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
18+
19+
some_list = []
20+
some_list.insert(index=0, object=val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
21+
22+
deque_like = []
23+
deque_like.insert(0, "bad") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
24+
25+
[1, 2, 3].insert(0, 9) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
26+
27+
class MyList(list):
28+
pass
29+
30+
custom_list = MyList()
31+
custom_list.insert(0, 'z') # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
32+
33+
def wrapper():
34+
lst = []
35+
lst.insert(0, "wrapped") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
36+
37+
38+
dq = deque()
39+
dq.appendleft("start")
40+
41+
mylist = []
42+
mylist.insert(0.0, val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
43+
44+
45+
data = []
46+
data.insert(1, "something")
47+
48+
real_deque = deque()
49+
real_deque.appendleft("good")
50+
51+
52+
other_list = []
53+
position = 1
54+
other_list.insert(position, "ok")
55+
56+
57+
58+
val = "new"
59+
queue = deque()
60+
queue.appendleft(val)
61+

src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
5050
AvoidNonPinnedMemoryForDataloaders.class,
5151
AvoidConvBiasBeforeBatchNorm.class,
5252
DisableGradientForModelEval.class,
53-
StringConcatenation.class
53+
StringConcatenation.class,
54+
PreferAppendLeft.class,
55+
AvoidCreatingTensorUsingNumpyOrNativePython.class
5456
);
5557

5658
public static final String LANGUAGE = "py";
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.python.checks;
19+
20+
import org.greencodeinitiative.creedengo.python.utils.UtilsAST;
21+
import org.sonar.check.Rule;
22+
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
23+
import org.sonar.plugins.python.api.tree.Tree;
24+
import org.sonar.plugins.python.api.tree.CallExpression;
25+
import org.sonar.plugins.python.api.tree.RegularArgument;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
import static java.util.Map.entry;
31+
import static org.sonar.plugins.python.api.tree.Tree.Kind.CALL_EXPR;
32+
33+
@Rule(key = "GCI104")
34+
public class AvoidCreatingTensorUsingNumpyOrNativePython extends PythonSubscriptionCheck {
35+
36+
private static final String DATA_ARGUMENT_NAME = "data";
37+
private static final int DATA_ARGUMENT_POSITION = 0;
38+
private static final Map<String, String> TORCH_OTHER_FUNCTIONS_MAPPING = Map.ofEntries(
39+
entry("numpy.random.rand", "torch.rand"),
40+
entry("numpy.random.randint", "torch.randint"),
41+
entry("numpy.random.randn", "torch.randn"),
42+
entry("numpy.zeros", "torch.zeros"),
43+
entry("numpy.zeros_like", "torch.zeros_like"),
44+
entry("numpy.ones", "torch.ones"),
45+
entry("numpy.ones_like", "torch.ones_like"),
46+
entry("numpy.full", "torch.full"),
47+
entry("numpy.full_like", "torch.full_like"),
48+
entry("numpy.eye", "torch.eye"),
49+
entry("numpy.arange", "torch.arange"),
50+
entry("numpy.linspace", "torch.linspace"),
51+
entry("numpy.logspace", "torch.logspace"),
52+
entry("numpy.identity", "torch.eye"),
53+
entry("numpy.tile", "torch.tile")
54+
);
55+
private static final List<String> TORCH_TENSOR_CONSTRUCTORS = List.of(
56+
"torch.tensor", "torch.FloatTensor",
57+
"torch.DoubleTensor", "torch.HalfTensor",
58+
"torch.BFloat16Tensor", "torch.ByteTensor",
59+
"torch.CharTensor", "torch.ShortTensor",
60+
"torch.IntTensor", "torch.LongTensor",
61+
"torch.BoolTensor", "torch.cuda.FloatTensor",
62+
"torch.cuda.DoubleTensor", "torch.cuda.HalfTensor",
63+
"torch.cuda.BFloat16Tensor", "torch.cuda.ByteTensor",
64+
"torch.cuda.CharTensor", "torch.cuda.ShortTensor",
65+
"torch.cuda.IntTensor", "torch.cuda.LongTensor",
66+
"torch.cuda.BoolTensor");
67+
protected static final String MESSAGE = "Directly create tensors as torch.Tensor instead of using numpy functions.";
68+
69+
@Override
70+
public void initialize(Context context) {
71+
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> {
72+
CallExpression callExpression = (CallExpression) ctx.syntaxNode();
73+
74+
if (TORCH_TENSOR_CONSTRUCTORS.contains(UtilsAST.getQualifiedName(callExpression))) {
75+
RegularArgument tensorCreatorArgument = UtilsAST.nthArgumentOrKeyword(DATA_ARGUMENT_POSITION, DATA_ARGUMENT_NAME, callExpression.arguments());
76+
if (tensorCreatorArgument != null && tensorCreatorArgument.expression().is(CALL_EXPR)) {
77+
String functionQualifiedName = UtilsAST.getQualifiedName((CallExpression) tensorCreatorArgument.expression());
78+
if (TORCH_OTHER_FUNCTIONS_MAPPING.containsKey(functionQualifiedName)) {
79+
ctx.addIssue(callExpression, MESSAGE);
80+
}
81+
}
82+
}
83+
});
84+
}
85+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.python.checks;
19+
20+
import org.sonar.check.Rule;
21+
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
22+
import org.sonar.plugins.python.api.SubscriptionContext;
23+
import org.sonar.plugins.python.api.tree.CallExpression;
24+
import org.sonar.plugins.python.api.tree.Expression;
25+
import org.sonar.plugins.python.api.tree.QualifiedExpression;
26+
import org.sonar.plugins.python.api.tree.RegularArgument;
27+
import org.sonar.plugins.python.api.tree.NumericLiteral;
28+
29+
import java.util.List;
30+
31+
import static org.sonar.plugins.python.api.tree.Tree.Kind.*;
32+
33+
@Rule(key = "GCI108")
34+
public class PreferAppendLeft extends PythonSubscriptionCheck {
35+
36+
public static final String DESCRIPTION = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
37+
38+
@Override
39+
public void initialize(Context context) {
40+
context.registerSyntaxNodeConsumer(CALL_EXPR, this::visitCallExpression);
41+
}
42+
43+
private void visitCallExpression(SubscriptionContext context) {
44+
CallExpression callExpression = (CallExpression) context.syntaxNode();
45+
if (callExpression.callee().is(QUALIFIED_EXPR)) {
46+
QualifiedExpression qualifiedExpression = (QualifiedExpression) callExpression.callee();
47+
if (qualifiedExpression.name().name().equals("insert")) {
48+
List<org.sonar.plugins.python.api.tree.Argument> arguments = callExpression.arguments();
49+
if (arguments.size() >= 2 && arguments.get(0) instanceof RegularArgument) {
50+
Expression firstArg = ((RegularArgument) arguments.get(0)).expression();
51+
if (firstArg.is(NUMERIC_LITERAL) && isZeroLiteral(firstArg)) {
52+
context.addIssue(callExpression, DESCRIPTION);
53+
}
54+
}
55+
}
56+
}
57+
}
58+
59+
private boolean isZeroLiteral(Expression expression) {
60+
if (expression.is(NUMERIC_LITERAL)) {
61+
NumericLiteral numericLiteral = (NumericLiteral) expression;
62+
String value = numericLiteral.valueAsString();
63+
try {
64+
return Double.parseDouble(value) == 0.0;
65+
} catch (NumberFormatException e) {
66+
return false;
67+
}
68+
}
69+
return false;
70+
}
71+
}

src/main/java/org/greencodeinitiative/creedengo/python/utils/UtilsAST.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ public static String getQualifiedName(CallExpression callExpression) {
5252
.orElse("");
5353
}
5454

55+
/**
56+
* Retrieves the variable name from the given SubscriptionContext.
57+
*
58+
* This method traverses the syntax tree of the provided context to locate
59+
* the nearest assignment statement. If an assignment statement is found,
60+
* it extracts the name of the variable on the left-hand side of the assignment.
61+
*
62+
* @param context The SubscriptionContext containing the syntax node to analyze.
63+
* It may be null or contain a null syntax node, in which case
64+
* the method returns null.
65+
* @return The name of the variable on the left-hand side of the assignment
66+
* statement, or null if no valid variable name can be determined.
67+
*/
5568
public static String getVariableName(SubscriptionContext context) {
5669

5770
if (context == null || context.syntaxNode() == null) {

src/main/resources/org/greencodeinitiative/creedengo/python/creedengo_way_profile.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
"GCI101",
1818
"GCI102",
1919
"GCI103",
20+
"GCI104",
2021
"GCI105",
2122
"GCI106",
2223
"GCI107",
24+
"GCI108",
2325
"GCI203",
2426
"GCI404"
2527
]

0 commit comments

Comments
 (0)