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
  • Loading branch information
maksim-grebeniuk-sonarsource committed Nov 13, 2024
1 parent d69535d commit a15572e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def local_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 @@ -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,18 @@ class Field(MetaField): ...
assertThat(symbol.hasUnresolvedTypeHierarchy()).isTrue();
}

@Test
void class_wth_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 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 a15572e

Please sign in to comment.