Skip to content

Commit

Permalink
SONARPY-2331 Implement setting metaclassFQN field for ClassType to Cl…
Browse files Browse the repository at this point in the history
…assDescriptor conversion (#2153)
  • Loading branch information
maksim-grebeniuk-sonarsource authored Nov 14, 2024
1 parent 9cabcdf commit da5face
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class ImportedWithoutMetaClassInherited(ImportedParentWithoutMetaClass):
def imported_inherited_foo(self) -> Any: # Noncompliant
...
class ImportedWithMetaClassInherited(ImportedParentWithMetaClass):
def imported_inherited_foo(self) -> Any: # FN SONARPY-2331
def imported_inherited_foo(self) -> Any: # Noncompliant
...
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.plugins.python.api.LocationInFile;

Expand Down Expand Up @@ -94,6 +95,7 @@ public boolean hasMetaClass() {
return hasMetaClass;
}

@CheckForNull
public String metaclassFQN() {
return metaclassFQN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,20 @@ private static Descriptor convert(String moduleFqn, String parentFqn, String sym
}
}

var metaclassFQN = type.metaClasses()
.stream()
.map(metaClass -> typeFqn(moduleFqn, metaClass))
.findFirst()
.orElse(null);

return new ClassDescriptor(symbolName, symbolFqn,
superClasses,
memberDescriptors,
type.hasDecorators(),
type.definitionLocation().orElse(null),
hasSuperClassWithoutDescriptor,
type.hasMetaClass(),
null,
metaclassFQN,
false
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,44 @@ class Field(MetaField): ...
assertThat(symbol.hasUnresolvedTypeHierarchy()).isTrue();
}

@Test
void class_wth_imported_metaclass() {
var code = """
from abc import ABCMeta
class WithMetaclass(metaclass=ABCMeta): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("abc.ABCMeta");
}

@Test
void class_wth_locally_defined_metaclass() {
var code = """
class LocalMetaClass: ...
class WithMetaclass(metaclass=LocalMetaClass): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("mod.LocalMetaClass");
}

@Test
void class_wth_unresolved_import_metaclass() {
var code = """
from unknown import UnresolvedMetaClass
class WithMetaclass(metaclass=UnresolvedMetaClass): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("unknown.UnresolvedMetaClass");
}

@Test
void projectPackages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ void testConvertClassType() {

// SONARPY-2307 support for superClass is missing in ClassType
assertThat(classDescriptor.hasSuperClassWithoutDescriptor()).isFalse();
// SONARPY-2307 support for metaclassFQN is missing in ClassType
assertThat(classDescriptor.metaclassFQN()).isNull();
assertThat(classDescriptor.metaclassFQN()).isEqualTo("int");
// SONARPY-2307 support for generics is missing in ClassType
assertThat(classDescriptor.supportsGenerics()).isFalse();
}
Expand Down

0 comments on commit da5face

Please sign in to comment.