1616 */
1717package org .sonar .python .checks ;
1818
19- import java .util .Map ;
20- import java .util .Optional ;
19+ import java .util .List ;
2120import org .sonar .check .Rule ;
2221import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
2322import org .sonar .plugins .python .api .SubscriptionContext ;
2423import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
25- import org .sonar .plugins .python .api .symbols .Symbol ;
2624import org .sonar .plugins .python .api .tree .QualifiedExpression ;
2725import org .sonar .plugins .python .api .tree .Tree ;
26+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatcher ;
27+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatchers ;
2828import org .sonar .python .quickfix .TextEditUtils ;
2929
3030@ Rule (key = "S6730" )
3131public class DeprecatedNumpyTypesCheck extends PythonSubscriptionCheck {
3232
3333 private static final String MESSAGE = "Replace this deprecated \" numpy\" type alias with the builtin type \" %s\" ." ;
3434 private static final String QUICK_FIX_MESSAGE = "Replace with %s." ;
35- private static final Map <String , String > TYPE_TO_CHECK = Map .of (
36- "numpy.bool" , "bool" ,
37- "numpy.int" , "int" ,
38- "numpy.float" , "float" ,
39- "numpy.complex" , "complex" ,
40- "numpy.object" , "object" ,
41- "numpy.str" , "str" ,
42- "numpy.long" , "int" ,
43- "numpy.unicode" , "str" );
35+
36+ private record DeprecatedType (TypeMatcher matcher , String replacement ) {}
37+
38+ private static final List <DeprecatedType > DEPRECATED_TYPES = List .of (
39+ new DeprecatedType (TypeMatchers .withFQN ("numpy.int" ), "int" ),
40+ new DeprecatedType (TypeMatchers .withFQN ("numpy.float" ), "float" ),
41+ new DeprecatedType (TypeMatchers .withFQN ("numpy.complex" ), "complex" ),
42+ new DeprecatedType (TypeMatchers .withFQN ("numpy.object" ), "object" ),
43+ new DeprecatedType (TypeMatchers .withFQN ("numpy.str" ), "str" ),
44+ new DeprecatedType (TypeMatchers .withFQN ("numpy.long" ), "int" ),
45+ new DeprecatedType (TypeMatchers .withFQN ("numpy.unicode" ), "str" ));
4446
4547 @ Override
4648 public void initialize (Context context ) {
@@ -50,13 +52,15 @@ public void initialize(Context context) {
5052
5153 private static void checkForDeprecatedTypesNames (SubscriptionContext ctx ) {
5254 QualifiedExpression expression = (QualifiedExpression ) ctx .syntaxNode ();
53- Optional .ofNullable (expression .symbol ())
54- .map (Symbol ::fullyQualifiedName )
55- .map (TYPE_TO_CHECK ::get )
56- .ifPresent (type -> raiseIssue (expression , type , ctx ));
55+ for (DeprecatedType deprecatedType : DEPRECATED_TYPES ) {
56+ if (deprecatedType .matcher ().isTrueFor (expression , ctx )) {
57+ raiseIssue (expression , deprecatedType .replacement (), ctx );
58+ return ;
59+ }
60+ }
5761 }
5862
59- private static void raiseIssue (Tree expression , String replacementType , SubscriptionContext ctx ) {
63+ private static void raiseIssue (QualifiedExpression expression , String replacementType , SubscriptionContext ctx ) {
6064 PreciseIssue issue = ctx .addIssue (expression , String .format (MESSAGE , replacementType ));
6165 PythonQuickFix quickFix = PythonQuickFix .newQuickFix (
6266 String .format (QUICK_FIX_MESSAGE , replacementType ),
0 commit comments