diff --git a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py index 554453bcb4..6ce70de030 100644 --- a/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py +++ b/python-checks/src/test/resources/checks/useOfAnyAsTypeHintImporting.py @@ -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 ... diff --git a/python-frontend/src/main/java/org/sonar/python/semantic/v2/converter/PythonTypeToDescriptorConverter.java b/python-frontend/src/main/java/org/sonar/python/semantic/v2/converter/PythonTypeToDescriptorConverter.java index 75f54cfd8b..3d1a265713 100644 --- a/python-frontend/src/main/java/org/sonar/python/semantic/v2/converter/PythonTypeToDescriptorConverter.java +++ b/python-frontend/src/main/java/org/sonar/python/semantic/v2/converter/PythonTypeToDescriptorConverter.java @@ -129,6 +129,12 @@ 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, @@ -136,7 +142,7 @@ private static Descriptor convert(String moduleFqn, String parentFqn, String sym type.definitionLocation().orElse(null), hasSuperClassWithoutDescriptor, type.hasMetaClass(), - null, + metaclassFQN, false ); } diff --git a/python-frontend/src/test/java/org/sonar/python/semantic/ProjectLevelSymbolTableTest.java b/python-frontend/src/test/java/org/sonar/python/semantic/ProjectLevelSymbolTableTest.java index b80fdb18b2..c60bcf6a4a 100644 --- a/python-frontend/src/test/java/org/sonar/python/semantic/ProjectLevelSymbolTableTest.java +++ b/python-frontend/src/test/java/org/sonar/python/semantic/ProjectLevelSymbolTableTest.java @@ -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() {