Skip to content

Commit

Permalink
SONARPY-1257 fix quality flaws (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim-grebeniuk-sonarsource authored Feb 3, 2023
1 parent 19db2c9 commit 0424db1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ public abstract class AbstractUnreadPrivateMembersCheck extends PythonSubscripti
@Override
public void initialize(Context context) {
String memberPrefix = memberPrefix();
context.registerSyntaxNodeConsumer(CLASSDEF, ctx -> {
Optional.of(ctx.syntaxNode())
.map(ClassDef.class::cast)
// avoid checking for classes with decorators since it is impossible to analyze its final behavior
.filter(classDef -> classDef.decorators().isEmpty())
.map(TreeUtils::getClassSymbolFromDef)
.map(ClassSymbol::declaredMembers)
.stream()
.flatMap(Collection::stream)
.filter(s -> s.name().startsWith(memberPrefix) && !s.name().endsWith("__") && equalsToKind(s) && isNeverRead(s))
.filter(Predicate.not(this::isException))
.forEach(symbol -> reportIssue(ctx, symbol));
});
context.registerSyntaxNodeConsumer(CLASSDEF, ctx -> Optional.of(ctx.syntaxNode())
.map(ClassDef.class::cast)
// avoid checking for classes with decorators since it is impossible to analyze its final behavior
.filter(classDef -> classDef.decorators().isEmpty())
.map(TreeUtils::getClassSymbolFromDef)
.map(ClassSymbol::declaredMembers)
.stream()
.flatMap(Collection::stream)
.filter(s -> s.name().startsWith(memberPrefix) && !s.name().endsWith("__") && equalsToKind(s) && isNeverRead(s))
.filter(Predicate.not(this::isException))
.forEach(symbol -> reportIssue(ctx, symbol)));
}

protected boolean isException(Symbol symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,6 @@ public SubscriptionContext ctx() {
}
}

/**
* In the case of unpacking expression, we cannot generate flows at the moment.
* However, to avoid a wrong interpretation of the unpacked expression in the context of absent arguments,
* an alternative dummy must be returned, which should not lead to false positives.
* The resolving of such expressions can be improved by <a href="https://sonarsource.atlassian.net/browse/SONARPY-1164">SONARPY-1164</a> if necessary.
*/
static class UnresolvedExpressionFlow extends ExpressionFlow {

private UnresolvedExpressionFlow(SubscriptionContext ctx) {
super(ctx, new LinkedList<>());
}
}

/**
* Dataclass to store a resolved KeyValuePair structure
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ private BuiltinSymbols() {
// empty constructor
}


public static final String CLASS_METHOD_DECORATOR = "classmethod";
public static final String STATIC_METHOD_DECORATOR = "staticmethod";

/**
* See https://docs.python.org/3/library/constants.html#built-in-consts
*/
Expand Down Expand Up @@ -70,7 +74,7 @@ private BuiltinSymbols() {
"enumerate",
"input",
"oct",
"staticmethod",
STATIC_METHOD_DECORATOR,
"bool",
"eval",
"int",
Expand Down Expand Up @@ -101,7 +105,7 @@ private BuiltinSymbols() {
"list",
"range",
"vars",
"classmethod",
CLASS_METHOD_DECORATOR,
"getattr",
"locals",
"repr",
Expand All @@ -124,7 +128,7 @@ private BuiltinSymbols() {
"divmod",
"input",
"open",
"staticmethod",
STATIC_METHOD_DECORATOR,
"all",
"enumerate",
"int",
Expand Down Expand Up @@ -165,7 +169,7 @@ private BuiltinSymbols() {
"long",
"reload",
"vars",
"classmethod",
CLASS_METHOD_DECORATOR,
"getattr",
"map",
"repr",
Expand Down Expand Up @@ -339,8 +343,5 @@ public static Set<String> all() {
return all;
}

public static final String CLASS_METHOD_DECORATOR = "classmethod";
public static final String STATIC_METHOD_DECORATOR = "staticmethod";

public static final Set<String> STATIC_AND_CLASS_METHOD_DECORATORS = Set.of(CLASS_METHOD_DECORATOR, STATIC_METHOD_DECORATOR);
}

0 comments on commit 0424db1

Please sign in to comment.