Skip to content

Commit

Permalink
SONARPY-2370 Fix FP on S5644 for generic types defined through type p…
Browse files Browse the repository at this point in the history
…arameter syntax (#2209)
  • Loading branch information
ghislainpiot authored Dec 5, 2024
1 parent f251712 commit 5390bf6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@
*/
package org.sonar.python.checks;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.plugins.python.api.LocationInFile;
import org.sonar.plugins.python.api.symbols.ClassSymbol;
import org.sonar.plugins.python.api.symbols.FunctionSymbol;
import org.sonar.plugins.python.api.symbols.Symbol;
import org.sonar.plugins.python.api.tree.ArgList;
import org.sonar.plugins.python.api.tree.CallExpression;
import org.sonar.plugins.python.api.tree.ClassDef;
import org.sonar.plugins.python.api.tree.Expression;
import org.sonar.plugins.python.api.tree.HasSymbol;
import org.sonar.plugins.python.api.tree.RegularArgument;
import org.sonar.plugins.python.api.tree.SubscriptionExpression;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.types.InferredType;
import org.sonar.python.tree.TreeUtils;
import org.sonar.python.types.InferredTypes;

import static org.sonar.plugins.python.api.symbols.Symbol.Kind.CLASS;
Expand All @@ -39,37 +45,62 @@ public class ItemOperationsTypeCheck extends ItemOperationsType {

@Override
public boolean isValidSubscription(Expression subscriptionObject, String requiredMethod, @Nullable String classRequiredMethod,
Map<LocationInFile, String> secondaries) {
Map<LocationInFile, String> secondaries) {

if (subscriptionObject.is(Tree.Kind.GENERATOR_EXPR)) {
return false;
}
if (subscriptionObject.is(Tree.Kind.CALL_EXPR)) {
Symbol subscriptionCalleeSymbol = ((CallExpression) subscriptionObject).calleeSymbol();
if (subscriptionCalleeSymbol != null && subscriptionCalleeSymbol.is(FUNCTION) && ((FunctionSymbol) subscriptionCalleeSymbol).isAsynchronous()) {
FunctionSymbol functionSymbol = (FunctionSymbol) subscriptionCalleeSymbol;
secondaries.put(functionSymbol.definitionLocation(), String.format(SECONDARY_MESSAGE, functionSymbol.name()));
return false;
}
if (isInvalidSubscriptionCallExpr(subscriptionObject, secondaries)) {
return false;
}
if (subscriptionObject instanceof HasSymbol hasSymbol) {
Symbol symbol = hasSymbol.symbol();
if (symbol == null || isTypingOrCollectionsSymbol(symbol)) {

var symbolOptional = TreeUtils.getSymbolFromTree(subscriptionObject);

if (symbolOptional.isPresent()) {
var symbol = symbolOptional.get();
if (isTypingOrCollectionsSymbol(symbol)) {
return true;
}
if (symbol.is(FUNCTION, CLASS)) {
secondaries.put(symbol.is(FUNCTION) ?
((FunctionSymbol) symbol).definitionLocation() : ((ClassSymbol) symbol).definitionLocation(), String.format(SECONDARY_MESSAGE, symbol.name()));
return canHaveMethod(symbol, requiredMethod, classRequiredMethod);
return isValidSubscriptionSymbol(symbol, subscriptionObject, secondaries, requiredMethod, classRequiredMethod);
}
}

InferredType type = subscriptionObject.type();
String typeName = InferredTypes.typeName(type);
String secondaryMessage = typeName != null ? String.format(SECONDARY_MESSAGE, typeName) : DEFAULT_SECONDARY_MESSAGE;
secondaries.put(typeClassLocation(type), secondaryMessage);
return type.canHaveMember(requiredMethod);
}

private static boolean isValidSubscriptionSymbol(Symbol symbol, Expression subscriptionObject, Map<LocationInFile, String> secondaries, String requiredMethod,
@Nullable String classRequiredMethod) {
LocationInFile locationInFile = symbol.is(FUNCTION) ? ((FunctionSymbol) symbol).definitionLocation() : ((ClassSymbol) symbol).definitionLocation();
secondaries.put(locationInFile, SECONDARY_MESSAGE.formatted(symbol.name()));
return isSubscriptionInClassArg(subscriptionObject) || canHaveMethod(symbol, requiredMethod, classRequiredMethod);
}

private static boolean isInvalidSubscriptionCallExpr(Expression expression, Map<LocationInFile, String> secondaries) {
if (expression instanceof CallExpression callExpression
&& callExpression.calleeSymbol() instanceof FunctionSymbol functionSymbol
&& functionSymbol.isAsynchronous()) {
secondaries.put(functionSymbol.definitionLocation(), SECONDARY_MESSAGE.formatted(functionSymbol.name()));
return true;
}
return false;
}

private static boolean isSubscriptionInClassArg(Expression subscriptionObject) {
return Optional.ofNullable(((ClassDef) TreeUtils.firstAncestorOfKind(subscriptionObject, Tree.Kind.CLASSDEF))).map(ClassDef::args).map(ArgList::arguments)
.stream()
.flatMap(Collection::stream)
.flatMap(TreeUtils.toStreamInstanceOfMapper(RegularArgument.class))
.map(RegularArgument::expression)
.flatMap(TreeUtils.toStreamInstanceOfMapper(SubscriptionExpression.class))
.map(SubscriptionExpression::object)
.anyMatch(subscriptionObject::equals);
}

@Override
public String message(@Nullable String name, String missingMethod) {
if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ class ExtendedMock(Mock):

def custom_mock():
a = ExtendedMock()[42]


class MyGenericClass[T]: ...

class MyGenericSubType(MyGenericClass[str]): ...

0 comments on commit 5390bf6

Please sign in to comment.