Skip to content

Commit

Permalink
SONARPY-2311 Fix setting null items to ClassDescriptor.superClasses f…
Browse files Browse the repository at this point in the history
…ield (#2139)
  • Loading branch information
maksim-grebeniuk-sonarsource authored Nov 7, 2024
1 parent 4d082a2 commit 02ac99b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ private static Descriptor convert(String moduleFqn, String parentFqn, String sym
var hasSuperClassWithoutDescriptor = false;
var superClasses = new ArrayList<String>();
for (var superClassWrapper : type.superClasses()) {
var superClass = superClassWrapper.type();
if (superClass != PythonType.UNKNOWN) {
var superClassFqn = typeFqn(moduleFqn, superClass);
var superClassFqn = typeFqn(moduleFqn, superClassWrapper.type());
if (superClassFqn != null) {
superClasses.add(superClassFqn);
} else {
hasSuperClassWithoutDescriptor = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,45 @@ void descriptorsForModule() {
assertThat(recomputedDescriptors).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrderElementsOf(retrievedDescriptors);
}

@Test
void superclasses_without_descriptor() {
var code = """
class MetaField: ...
class Field(MetaField()): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));

var descriptors = projectSymbolTable.getDescriptorsFromModule("mod");
assertThat(descriptors).hasSize(2);

var fieldClassDescriptor = descriptors.stream().filter(d -> "Field".equals(d.name()))
.map(ClassDescriptor.class::cast)
.findFirst()
.orElse(null);
assertThat(fieldClassDescriptor).isNotNull();
assertThat(fieldClassDescriptor.superClasses()).isEmpty();
assertThat(fieldClassDescriptor.hasSuperClassWithoutDescriptor()).isTrue();
var symbol = (ClassSymbol) projectSymbolTable.getSymbol("mod.Field");
assertThat(symbol.superClasses()).isEmpty();
assertThat(symbol.hasUnresolvedTypeHierarchy()).isTrue();
}

@Test
void superclasses_without_descriptor_unresolved_import() {
var code = """
from unknown import MetaField
class Field(MetaField): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbol) projectSymbolTable.getSymbol("mod.Field");
assertThat(symbol.hasUnresolvedTypeHierarchy()).isTrue();
}


@Test
void projectPackages() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
Expand Down

0 comments on commit 02ac99b

Please sign in to comment.