Skip to content

Commit 3bbbd8e

Browse files
Fix quality flaws (SonarSource#860)
1 parent 8642e1f commit 3bbbd8e

6 files changed

Lines changed: 8 additions & 26 deletions

File tree

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,4 @@ public static boolean classHasInheritance(ClassDef classDef) {
9292
}
9393
return arguments.size() != 1 || !"object".equals(arguments.get(0).firstToken().value());
9494
}
95-
96-
public static boolean isCalledInClassBody(Symbol symbol, ClassDef classDef) {
97-
return symbol.usages().stream().anyMatch(usage -> {
98-
Tree tree = usage.tree();
99-
Tree parentTree = tree.parent();
100-
101-
if (!parentTree.is(Tree.Kind.CALL_EXPR)) {
102-
return false;
103-
}
104-
105-
// We want a call expression which
106-
// 1) is a call to our function
107-
// 2) is not in a call within another function
108-
// 3) it has a class body ancestor AND it is classDef
109-
CallExpression call = (CallExpression) parentTree;
110-
return call.callee().equals(tree)
111-
&& TreeUtils.firstAncestorOfKind(call, Tree.Kind.FUNCDEF) == null
112-
&& classDef.equals(TreeUtils.firstAncestorOfKind(call, Tree.Kind.CLASSDEF));
113-
});
114-
}
115-
11695
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private static void raiseIssues(SubscriptionContext ctx, FunctionDef functionDef
6868
String functionName = functionDef.name().name();
6969
String returnTypeName = InferredTypes.typeName(declaredReturnType);
7070
if (!returnTypeVisitor.yieldStatements.isEmpty()) {
71+
// Here we should probably use an equivalent of "canBeOrExtend" (accepting uncertainty) instead of "mustBeOrExtend"
7172
if (ITERABLE_TYPES.stream().anyMatch(declaredReturnType::mustBeOrExtend)) {
7273
return;
7374
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.sonar.plugins.python.api.tree.AssignmentStatement;
3030
import org.sonar.plugins.python.api.tree.DelStatement;
3131
import org.sonar.plugins.python.api.tree.Expression;
32-
import org.sonar.plugins.python.api.tree.Name;
3332
import org.sonar.plugins.python.api.tree.SubscriptionExpression;
3433
import org.sonar.plugins.python.api.tree.Tree;
3534
import org.sonar.python.tree.TreeUtils;

python-checks/src/test/resources/checks/confusingTypeChecking/incorrectExceptionType.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Union, Optional
12
class A: ...
23
class CustomException(BaseException): ...
34
class AnotherException(CustomException): ...
@@ -12,9 +13,11 @@ def custom(param1: A, param2: CustomException, param3: AnotherException):
1213
raise param3
1314

1415

15-
def builtin(param1: str, param2: Exception, param3: BaseException, param4: ValueError):
16+
def builtin(param1: str, param2: Exception, param3: BaseException, param4: ValueError, param5: Union[BaseException, str], param6: Optional[BaseException]):
1617
raise param1 # Noncompliant {{Fix this "raise" statement; Previous type checks suggest that "param1" has type "str" and is not an exception.}}
1718
raise param2
1819
raise param3
1920
raise param4
2021
raise my_int() # Noncompliant {{Fix this "raise" statement; Previous type checks suggest that this expression has type "int" and is not an exception.}}
22+
raise param5
23+
raise param6

python-frontend/src/main/java/org/sonar/python/types/DeclaredType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,12 @@ public boolean isCompatibleWith(InferredType other) {
125125

126126
@Override
127127
public boolean mustBeOrExtend(String typeName) {
128-
// TODO: change its implementation to reflect semantic of 'RuntimeType.canOnlyBe'
129128
return alternativeTypeSymbols().stream().flatMap(a -> {
130129
if (a.is(Symbol.Kind.AMBIGUOUS)) {
131-
return ((AmbiguousSymbol) a).alternatives().stream().filter(alternative -> alternative.is(Symbol.Kind.CLASS));
130+
return ((AmbiguousSymbol) a).alternatives().stream().filter(alternative -> alternative.is(CLASS));
132131
}
133132
return Stream.of(a);
134-
}).filter(a -> a.is(Symbol.Kind.CLASS)).allMatch(a -> ((ClassSymbol) a).isOrExtends(typeName));
133+
}).filter(a -> a.is(CLASS)).allMatch(a -> ((ClassSymbol) a).isOrExtends(typeName));
135134
}
136135

137136
@Override

python-frontend/src/test/java/org/sonar/python/semantic/SelfSymbolImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void removeUsages() {
4747
((SymbolImpl) selfSymbol).removeUsages();
4848
assertThat(selfSymbol.usages()).isEmpty();
4949
ClassDef classDef = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Tree.Kind.CLASSDEF));
50+
assertThat(classDef.instanceFields()).isNotEmpty();
5051
assertThat(classDef.instanceFields()).allMatch(symbol -> symbol.usages().isEmpty());
5152
}
5253

0 commit comments

Comments
 (0)