Skip to content

Commit bc06ab6

Browse files
authored
SONARPY-2094: Refactor Python version comparison to PythonVersionsUtils (#2049)
1 parent f5c8971 commit bc06ab6

File tree

9 files changed

+28
-35
lines changed

9 files changed

+28
-35
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ private static boolean isTheNestingTooDeepInFormatSpecifier(@Nullable FormatSpec
104104
}
105105

106106
private static boolean supportsTypeParameterSyntax(SubscriptionContext ctx) {
107-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_312;
108-
109-
// All versions must be greater than or equal to the required version.
110-
return ctx.sourcePythonVersions().stream()
111-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
107+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_312);
112108
}
113109
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ private static boolean isTypingGeneric(Expression expression) {
106106
}
107107

108108
private static boolean supportsTypeParameterSyntax(SubscriptionContext ctx) {
109-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_312;
110-
111-
// All versions must be greater than or equal to the required version.
112-
return ctx.sourcePythonVersions().stream()
113-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
109+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_312);
114110
}
115111
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ private static Set<Tree> getAssignmentLocations(Set<Tree> secondaryLocations) {
8282
}
8383

8484
private static boolean supportsTypeParameterSyntax(SubscriptionContext ctx) {
85-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_312;
86-
87-
// All versions must be greater than or equal to the required version.
88-
return ctx.sourcePythonVersions().stream()
89-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
85+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_312);
9086
}
9187
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ private static void checkUseOfGenerics(SubscriptionContext ctx) {
7272
return;
7373
}
7474
TypeAliasStatement typeStatement = (TypeAliasStatement) ctx.syntaxNode();
75-
7675
Set<Tree> typeVarAsTypeParameter = Optional.ofNullable(typeStatement.expression())
7776
.flatMap(TreeUtils.toOptionalInstanceOfMapper(SubscriptionExpression.class))
7877
.map(SubscriptionExpression::subscripts)
@@ -96,12 +95,7 @@ private static Set<Tree> getAssignmentLocations(Set<Tree> secondaryLocations) {
9695
.collect(Collectors.toSet());
9796
}
9897

99-
10098
private static boolean supportsTypeParameterSyntax(SubscriptionContext ctx) {
101-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_312;
102-
103-
// All versions must be greater than or equal to the required version.
104-
return ctx.sourcePythonVersions().stream()
105-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
99+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_312);
106100
}
107101
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
@Rule(key = "S6890")
3838
public class PytzUsageCheck extends PythonSubscriptionCheck {
39-
private static final PythonVersionUtils.Version REQUIRED_VERSION = PythonVersionUtils.Version.V_39;
4039
private static final String MESSAGE = "Don't use `pytz` module with Python 3.9 and later.";
4140

4241
@Override
@@ -46,8 +45,7 @@ public void initialize(Context context) {
4645
}
4746

4847
private static boolean isRelevantPythonVersion(SubscriptionContext context) {
49-
return context.sourcePythonVersions().stream()
50-
.allMatch(version -> version.compare(REQUIRED_VERSION.major(), REQUIRED_VERSION.minor()) >= 0);
48+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(context.sourcePythonVersions(), PythonVersionUtils.Version.V_39);
5149
}
5250

5351
private static void checkImport(SubscriptionContext context) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ public static void checkTypeAliasVariableAnnotation(SubscriptionContext ctx) {
5454
}
5555

5656
private static boolean supportsTypeParameterSyntax(SubscriptionContext ctx) {
57-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_312;
58-
59-
// All versions must be greater than or equal to the required version.
60-
return ctx.sourcePythonVersions().stream()
61-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
57+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_312);
6258
}
6359
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ private static void checkTypeAnnotation(SubscriptionContext ctx) {
6060
}
6161

6262
private static boolean supportsUnionTypeExpressions(SubscriptionContext ctx) {
63-
PythonVersionUtils.Version required = PythonVersionUtils.Version.V_310;
64-
65-
// All versions must be greater than or equal to the required version.
66-
return ctx.sourcePythonVersions().stream()
67-
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
63+
return PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(ctx.sourcePythonVersions(), PythonVersionUtils.Version.V_310);
6864
}
6965
}

python-frontend/src/main/java/org/sonar/plugins/python/api/PythonVersionUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private PythonVersionUtils() {
108108
public static Set<Version> fromString(String propertyValue) {
109109
return fromStringArray(propertyValue.split(","));
110110
}
111+
111112
public static Set<Version> fromStringArray(String[] versions) {
112113
if (versions.length == 0) {
113114
return allVersions();
@@ -168,6 +169,12 @@ private static boolean guessPythonVersion(Set<Version> pythonVersions, String ve
168169
return true;
169170
}
170171

172+
public static boolean areSourcePythonVersionsGreaterOrEqualThan(Set<Version> sourcePythonVersions, Version required) {
173+
// All versions must be greater than or equal to the required version.
174+
return !sourcePythonVersions.isEmpty() && sourcePythonVersions.stream()
175+
.allMatch(version -> version.compare(required.major(), required.minor()) >= 0);
176+
}
177+
171178
private static void logErrorMessage(String propertyValue) {
172179
LOG.warn(
173180
"Error while parsing value of parameter '{}' ({}). Versions must be specified as MAJOR_VERSION.MINOR_VERSION (e.g. \"3.7, 3.8\")",

python-frontend/src/test/java/org/sonar/plugins/python/api/PythonVersionUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
*/
2020
package org.sonar.plugins.python.api;
2121

22+
import java.util.Set;
2223
import org.junit.jupiter.api.Test;
2324
import org.junit.jupiter.api.extension.RegisterExtension;
2425
import org.slf4j.event.Level;
2526
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertTrue;
2831
import static org.sonar.plugins.python.api.PythonVersionUtils.Version.V_310;
2932
import static org.sonar.plugins.python.api.PythonVersionUtils.Version.V_311;
3033
import static org.sonar.plugins.python.api.PythonVersionUtils.Version.V_312;
@@ -75,4 +78,15 @@ void error_while_parsing_version() {
7578
assertThat(logTester.logs(Level.WARN))
7679
.contains("Error while parsing value of parameter 'sonar.python.version' (foo). Versions must be specified as MAJOR_VERSION.MINOR_VERSION (e.g. \"3.7, 3.8\")");
7780
}
81+
82+
@Test
83+
void isPythonVersionGreaterOrEqualThan() {
84+
assertFalse(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(), V_39));
85+
assertFalse(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(V_36, V_38), V_39));
86+
assertFalse(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(V_36, V_310), V_39));
87+
assertTrue(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(V_39), V_39));
88+
assertTrue(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(V_39, V_310), V_39));
89+
assertTrue(PythonVersionUtils.areSourcePythonVersionsGreaterOrEqualThan(Set.of(V_312, V_310), V_39));
90+
}
91+
7892
}

0 commit comments

Comments
 (0)