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 15e4093
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 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

0 comments on commit 15e4093

Please sign in to comment.