Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim-grebeniuk-sonarsource committed Nov 14, 2024
1 parent 04c4e5d commit 510214f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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 @@ -1126,7 +1126,7 @@ class Field(MetaField): ...
}

@Test
void class_wth_metaclass() {
void class_wth_imported_metaclass() {
var code = """
from abc import ABCMeta
class WithMetaclass(metaclass=ABCMeta): ...
Expand All @@ -1138,6 +1138,32 @@ class WithMetaclass(metaclass=ABCMeta): ...
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() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
Expand Down

0 comments on commit 510214f

Please sign in to comment.