Skip to content

Commit

Permalink
SONARPY-2219 Class type to descriptor conversion super classes support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-serre-sonarsource committed Oct 21, 2024
1 parent 3670930 commit 10f3507
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import org.sonar.python.index.AmbiguousDescriptor;
import org.sonar.python.index.ClassDescriptor;
import org.sonar.python.index.Descriptor;
Expand All @@ -32,6 +33,7 @@
import org.sonar.python.types.v2.FunctionType;
import org.sonar.python.types.v2.ParameterV2;
import org.sonar.python.types.v2.PythonType;
import org.sonar.python.types.v2.TypeWrapper;
import org.sonar.python.types.v2.UnionType;
import org.sonar.python.types.v2.UnknownType;

Expand Down Expand Up @@ -85,8 +87,9 @@ private Descriptor convert(String moduleFqn, String symbolName, FunctionType typ

private Descriptor convert(String moduleFqn, String symbolName, ClassType type) {
Set<Descriptor> memberDescriptors = type.members().stream().map(m -> convert(moduleFqn, m.name(), m.type())).collect(Collectors.toSet());
List<String> superClasses = type.superClasses().stream().map(TypeWrapper::type).map(t -> typeFqn(moduleFqn, t)).collect(Collectors.toList());
return new ClassDescriptor(symbolName, symbolFqn(moduleFqn, symbolName),
List.of(),
superClasses,
memberDescriptors,
type.hasDecorators(),
type.definitionLocation().orElse(null),
Expand Down Expand Up @@ -115,13 +118,8 @@ private Descriptor convert(String moduleFqn, String symbolName, UnknownType.Unre
}

public FunctionDescriptor.Parameter convert(String moduleFqn, ParameterV2 parameter) {
String annotatedType = null;
var type = parameter.declaredType().type().unwrappedType();
if (type instanceof UnknownType.UnresolvedImportType importType) {
annotatedType = importType.importPath();
} else if (type instanceof ClassType classType) {
annotatedType = moduleFqn + "." + classType.name();
}
var annotatedType = typeFqn(moduleFqn, type);

return new FunctionDescriptor.Parameter(parameter.name(),
annotatedType,
Expand All @@ -133,9 +131,18 @@ public FunctionDescriptor.Parameter convert(String moduleFqn, ParameterV2 parame
parameter.location());
}

@CheckForNull
private static String typeFqn(String moduleFqn, PythonType type) {
if (type instanceof UnknownType.UnresolvedImportType importType) {
return importType.importPath();
} else if (type instanceof ClassType classType) {
return moduleFqn + "." + classType.name();
}
return null;
}

private static String symbolFqn(String moduleFqn, String symbolName) {
return moduleFqn + "." + symbolName;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class SonarLintPythonIndexerProjectLevelSymbolTableBuildingTest {
@Test
void single_file_simple_test() throws IOException {
var projectLevelSymbolTable = buildProjectLevelSymbolTable("script.py");
assertThat(projectLevelSymbolTable.getDescriptorsFromModule("script")).hasSize(3);
assertThat(projectLevelSymbolTable.getDescriptorsFromModule("script")).hasSize(4);
Set<Descriptor> moduleDescriptors = projectLevelSymbolTable.getDescriptorsFromModuleV2("script");
assertThat(moduleDescriptors).hasSize(3);
assertThat(moduleDescriptors).hasSize(4);

var aClassDescriptor = moduleDescriptors
.stream()
Expand All @@ -66,6 +66,7 @@ void single_file_simple_test() throws IOException {
.orElse(null);
assertThat(aClassDescriptor).isNotNull();
assertThat(aClassDescriptor.members()).hasSize(1);
assertThat(aClassDescriptor.superClasses()).containsOnly("script.Parent", "int");

var doSomethingDescriptor = aClassDescriptor.members()
.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
def foo(): ...

class A:
class Parent:
...

class A(Parent, int):
def do_something(self, a: int):
...


if something:
class B:
def method_one(self): ...
Expand Down

0 comments on commit 10f3507

Please sign in to comment.