Skip to content

Commit 1bc3a6f

Browse files
authored
SONARPY-1481 Rule S1244: Corrected quickfix to use not instead of ! (#1579)
1 parent 8dc323f commit 1bc3a6f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public class FloatingPointEqualityCheck extends PythonSubscriptionCheck {
4444

4545
private static final String MESSAGE = "Do not perform equality checks with floating point values.";
46-
private static final String QUICK_FIX_MESSAGE = "Replace with %s%s.isclose().";
46+
private static final String QUICK_FIX_MESSAGE = "Replace with \"%s%s.isclose()\".";
4747

4848
private static final String QUICK_FIX_MATH = "%s%s.isclose(%s, %s, rel_tol=1e-09, abs_tol=1e-09)";
4949

@@ -117,7 +117,7 @@ private boolean isBinaryOperationWithFloat(Expression expression) {
117117
}
118118

119119
private PythonQuickFix createQuickFix(BinaryExpression binaryExpression, String operator) {
120-
String notToken = "!=".equals(operator) ? "!" : "";
120+
String notToken = "!=".equals(operator) ? "not " : "";
121121
String isCloseModuleName = getModuleNameOrAliasForIsClose();
122122
String message = String.format(QUICK_FIX_MESSAGE, notToken, isCloseModuleName);
123123
Builder quickFix = PythonQuickFix.newQuickFix(message);

python-checks/src/test/java/org/sonar/python/checks/FloatingPointEqualityCheckTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void quickfixEquality(){
4444
" if math.isclose(a - 0.1, b, rel_tol=1e-09, abs_tol=1e-09):\n" +
4545
" ...";
4646
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
47-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with math.isclose().");
47+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"math.isclose()\".");
4848
}
4949

5050
@Test
@@ -72,10 +72,10 @@ void quickfixInequality(){
7272
String compliant =
7373
"import math\n" +
7474
"def foo(a,b):\n" +
75-
" if !math.isclose(a - 0.1, b, rel_tol=1e-09, abs_tol=1e-09):\n" +
75+
" if not math.isclose(a - 0.1, b, rel_tol=1e-09, abs_tol=1e-09):\n" +
7676
" ...";
7777
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
78-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with !math.isclose().");
78+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"not math.isclose()\".");
7979
}
8080

8181
@Test
@@ -121,7 +121,7 @@ void quickfixNumpyImport(){
121121
" if numpy.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
122122
" ...";
123123
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
124-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with numpy.isclose().");
124+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"numpy.isclose()\".");
125125
}
126126

127127
@Test
@@ -137,7 +137,7 @@ void quickfixMultipleImport(){
137137
" if math.isclose(a - 0.1, b, rel_tol=1e-09, abs_tol=1e-09):\n" +
138138
" ...";
139139
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
140-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with math.isclose().");
140+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"math.isclose()\".");
141141
}
142142

143143
@Test
@@ -155,7 +155,7 @@ void quickfixPrioritizeNumpyOverMath(){
155155
" if np.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
156156
" ...";
157157
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
158-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with np.isclose().");
158+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"np.isclose()\".");
159159
}
160160

161161
@Test
@@ -171,7 +171,7 @@ void quickfixPrioritizeTorchOverMath(){
171171
" if torch.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
172172
" ...";
173173
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
174-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with torch.isclose().");
174+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"torch.isclose()\".");
175175
}
176176

177177
@Test
@@ -187,7 +187,7 @@ void quickfixNumpyImportAlias(){
187187
" if np.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
188188
" ...";
189189
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
190-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with np.isclose().");
190+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"np.isclose()\".");
191191
}
192192

193193
@Test
@@ -203,7 +203,7 @@ void quickfixPyTorchImport(){
203203
" if torch.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
204204
" ...";
205205
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
206-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with torch.isclose().");
206+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"torch.isclose()\".");
207207
}
208208

209209
@Test
@@ -221,6 +221,6 @@ void quickfixTakeFirstImport(){
221221
" if torch.isclose(a - 0.1, b, rtol=1e-09, atol=1e-09):\n" +
222222
" ...";
223223
PythonQuickFixVerifier.verify(check, noncompliant, compliant);
224-
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with torch.isclose().");
224+
PythonQuickFixVerifier.verifyQuickFixMessages(check, noncompliant, "Replace with \"torch.isclose()\".");
225225
}
226226
}

0 commit comments

Comments
 (0)