Skip to content

Commit

Permalink
SONARPY-2256 Resolve fully qualified names for class members descript…
Browse files Browse the repository at this point in the history
…ors (#2104)
  • Loading branch information
maksim-grebeniuk-sonarsource committed Oct 25, 2024
1 parent 3007faf commit 6115ada
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class PythonTypeToDescriptorConverter {

public Descriptor convert(String moduleFqn, SymbolV2 symbol, Set<PythonType> types) {
var candidates = types.stream()
.map(type -> convert(moduleFqn, symbol.name(), type))
.map(type -> convert(moduleFqn, moduleFqn, symbol.name(), type))
.flatMap(candidate -> {
if (candidate instanceof AmbiguousDescriptor ambiguousDescriptor) {
return ambiguousDescriptor.alternatives().stream();
Expand All @@ -59,30 +59,30 @@ public Descriptor convert(String moduleFqn, SymbolV2 symbol, Set<PythonType> typ
return new AmbiguousDescriptor(symbol.name(), symbolFqn(moduleFqn, symbol.name()), candidates);
}

private Descriptor convert(String moduleFqn, String symbolName, PythonType type) {
private Descriptor convert(String moduleFqn, String parentFqn, String symbolName, PythonType type) {
if (type instanceof FunctionType functionType) {
return convert(moduleFqn, symbolName, functionType);
return convert(moduleFqn, parentFqn, symbolName, functionType);
}
if (type instanceof ClassType classType) {
return convert(moduleFqn, symbolName, classType);
return convert(moduleFqn, parentFqn, symbolName, classType);
}
if (type instanceof UnionType unionType) {
return convert(moduleFqn, symbolName, unionType);
return convert(moduleFqn, parentFqn, symbolName, unionType);
}
if (type instanceof UnknownType.UnresolvedImportType unresolvedImportType) {
return convert(moduleFqn, symbolName, unresolvedImportType);
return convert(parentFqn, symbolName, unresolvedImportType);
}
return new VariableDescriptor(symbolName, symbolFqn(moduleFqn, symbolName), null);
return new VariableDescriptor(symbolName, symbolFqn(parentFqn, symbolName), null);
}

private Descriptor convert(String moduleFqn, String symbolName, FunctionType type) {
private Descriptor convert(String moduleFqn, String parentFqn, String symbolName, FunctionType type) {

var parameters = type.parameters()
.stream()
.map(parameter -> convert(moduleFqn, parameter))
.toList();

return new FunctionDescriptor(symbolName, symbolFqn(moduleFqn, symbolName),
return new FunctionDescriptor(symbolName, symbolFqn(parentFqn, symbolName),
parameters,
type.isAsynchronous(),
type.isInstanceMethod(),
Expand All @@ -91,13 +91,18 @@ private Descriptor convert(String moduleFqn, String symbolName, FunctionType typ
type.definitionLocation().orElse(null),
null,
null
);
);
}

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());
private Descriptor convert(String moduleFqn, String parentFqn, String symbolName, ClassType type) {
var symbolFqn = symbolFqn(parentFqn, symbolName);
var memberDescriptors = type.members()
.stream()
.map(m -> convert(moduleFqn, symbolFqn, m.name(), m.type()))
.collect(Collectors.toSet());
List<String> superClasses = type.superClasses().stream().map(TypeWrapper::type).map(t -> typeFqn(moduleFqn, t)).toList();
return new ClassDescriptor(symbolName, symbolFqn(moduleFqn, symbolName),

return new ClassDescriptor(symbolName, symbolFqn,
superClasses,
memberDescriptors,
type.hasDecorators(),
Expand All @@ -109,19 +114,19 @@ private Descriptor convert(String moduleFqn, String symbolName, ClassType type)
);
}

private Descriptor convert(String moduleFqn, String symbolName, UnionType type) {
private Descriptor convert(String moduleFqn, String parentFqn, String symbolName, UnionType type) {
var candidates = type.candidates().stream()
.map(candidateType -> convert(moduleFqn, symbolName, candidateType))
.map(candidateType -> convert(moduleFqn, parentFqn, symbolName, candidateType))
.collect(Collectors.toSet());
return new AmbiguousDescriptor(symbolName,
symbolFqn(moduleFqn, symbolName),
candidates
);
}

private static Descriptor convert(String moduleFqn, String symbolName, UnknownType.UnresolvedImportType type) {
private static Descriptor convert(String parentFqn, String symbolName, UnknownType.UnresolvedImportType type) {
return new VariableDescriptor(symbolName,
symbolFqn(moduleFqn, symbolName),
symbolFqn(parentFqn, symbolName),
type.importPath()
);
}
Expand All @@ -135,8 +140,8 @@ private FunctionDescriptor.Parameter convert(String moduleFqn, ParameterV2 param
parameter.hasDefaultValue(),
parameter.isKeywordOnly(),
parameter.isPositionalOnly(),
parameter.isKeywordVariadic(),
parameter.isPositionalVariadic(),
parameter.isKeywordVariadic(),
parameter.location());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ void testConvertClassType() {
VariableDescriptor memberVariableDescriptor = (VariableDescriptor) memberDescriptor;
assertThat(memberVariableDescriptor.name()).isEqualTo("aMember");
assertThat(memberVariableDescriptor.annotatedType()).isEqualTo("int");
// TODO SONARPY-2222 expected fullyqualified name of the member is "foo.myClass.aMember"
assertThat(memberVariableDescriptor.fullyQualifiedName()).isEqualTo("foo.aMember");
assertThat(memberVariableDescriptor.fullyQualifiedName()).isEqualTo("foo.myClass.aMember");

assertThat(classDescriptor.hasDecorators()).isTrue();
assertThat(classDescriptor.definitionLocation()).isEqualTo(location);
Expand Down

0 comments on commit 6115ada

Please sign in to comment.