Skip to content

Commit

Permalink
Remove public constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainpiot committed Dec 2, 2024
1 parent 5fd3a1d commit ecff6b5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static PythonVisitorContext createNotebookContext(File file, Map<Integer,
}

public static ProjectLevelSymbolTable globalSymbols(List<File> files, File baseDir) {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
for (File file : files) {
var pythonFile = new TestPythonFile(file);
if (pythonFile.isIPython()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ProjectLevelSymbolTable empty() {
}

public static ProjectLevelSymbolTable from(Map<String, Set<Descriptor>> globalDescriptorsByModuleName) {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

for (var entry : globalDescriptorsByModuleName.entrySet()) {
var descriptors = entry.getValue();
Expand All @@ -75,7 +75,7 @@ public static ProjectLevelSymbolTable from(Map<String, Set<Descriptor>> globalDe
return projectLevelSymbolTable;
}

public ProjectLevelSymbolTable() {
private ProjectLevelSymbolTable() {
this.globalDescriptorsByModuleName = new HashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void imported_class_hasSuperClassWithoutSymbol() {
}

private static Set<Symbol> globalSymbols(FileInput fileInput, String packageName) {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addModule(fileInput, packageName, pythonFile("mod.py"));
return projectLevelSymbolTable.getSymbolsFromModule(packageName.isEmpty() ? "mod" : packageName + ".mod");
}
Expand All @@ -410,7 +410,7 @@ void test_remove_module() {
FileInput tree = parseWithoutSymbols(
"class A: pass"
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));
assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod")).extracting(Symbol::name).containsExactlyInAnyOrder("A");
projectLevelSymbolTable.removeModule("", "mod.py");
Expand All @@ -419,7 +419,7 @@ void test_remove_module() {

@Test
void test_insert_entry() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
VariableDescriptor variableDescriptor = new VariableDescriptor("x", "mod.x", null);
projectLevelSymbolTable.insertEntry("mod", Set.of(variableDescriptor));
assertThat(projectLevelSymbolTable.descriptorsForModule("mod")).containsExactly(variableDescriptor);
Expand All @@ -431,7 +431,7 @@ void test_add_module_after_creation() {
FileInput tree = parseWithoutSymbols(
"class A: pass"
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));
assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod")).extracting(Symbol::name).containsExactlyInAnyOrder("A");
assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod2")).isNull();
Expand All @@ -452,7 +452,7 @@ void test_imported_modules() {
FileInput tree = parseWithoutSymbols(
"import A"
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));
assertThat(projectLevelSymbolTable.importsByModule()).containsExactly(Map.entry("mod", Set.of("A")));
assertThat(projectLevelSymbolTable.getSymbolsFromModule("mod2")).isNull();
Expand Down Expand Up @@ -651,7 +651,7 @@ void class_symbol_inheritance_from_nested_class() {

@Test
void child_class_method_call_is_not_a_member_of_parent_class() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
FileInput importedFileInput = parseWithoutSymbols(
"class A:",
" def meth(self): ",
Expand Down Expand Up @@ -797,7 +797,7 @@ void loop_in_class_inheritance() {
"class B(A): ..."
};

ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(foo), "", pythonFile("foo.py"));
projectSymbolTable.addModule(parseWithoutSymbols(bar), "", pythonFile("bar.py"));

Expand Down Expand Up @@ -838,7 +838,7 @@ void symbols_from_module_should_be_the_same() {
"class A: ...",
"class B(A): ..."
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));
Set<Symbol> mod = projectLevelSymbolTable.getSymbolsFromModule("mod");
assertThat(mod).extracting(Symbol::name).containsExactlyInAnyOrder("A", "B");
Expand Down Expand Up @@ -890,7 +890,7 @@ void no_stackoverflow_for_ambiguous_descriptor() {
String[] bar = {
"from foo import *\n",
};
ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(foo), "", pythonFile("foo.py"));
projectSymbolTable.addModule(parseWithoutSymbols(bar), "", pythonFile("bar.py"));

Expand All @@ -914,7 +914,7 @@ void no_stackoverflow_for_ambiguous_descriptor() {

@Test
void ambiguous_descriptor_alternatives_dont_rely_on_FQN_for_conversion() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
Set<Descriptor> descriptors = new HashSet<>();
VariableDescriptor variableDescriptor = new VariableDescriptor("Ambiguous", "foo.Ambiguous", null);
ClassDescriptor classDescriptor = new ClassDescriptor("Ambiguous", "foo.Ambiguous", List.of(), Set.of(), false, null, false, false, null, false);
Expand Down Expand Up @@ -946,7 +946,7 @@ void loop_in_inheritance_with_method_paraneters_of_same_type() {
" def my_B_other_method(param: B): ..."
};

ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(foo), "", pythonFile("foo.py"));
projectSymbolTable.addModule(parseWithoutSymbols(bar), "", pythonFile("bar.py"));

Expand Down Expand Up @@ -979,7 +979,7 @@ void django_views() {
"def bar(): ..."
};

ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(urls), "", pythonFile("urls.py"));

assertThat(projectSymbolTable.isDjangoView("views.foo")).isTrue();
Expand Down Expand Up @@ -1011,7 +1011,7 @@ def qix(): ...
urlpatterns.append(path('bar', MyOtherClass.MyNestedClass.qix, name='bar'))
""";

ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(content), "my_package", pythonFile("urls.py"));
assertThat(projectSymbolTable.isDjangoView("my_package.urls.foo")).isTrue();
assertThat(projectSymbolTable.isDjangoView("my_package.urls.MyClass.bar")).isTrue();
Expand All @@ -1028,7 +1028,7 @@ def ambiguous(): ...
def ambiguous(): ...
urlpatterns = [path('bar', ambiguous, name='bar')]
""";
ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(content), "my_package", pythonFile("urls.py"));
assertThat(projectSymbolTable.isDjangoView("my_package.urls.ambiguous")).isFalse();
}
Expand All @@ -1040,7 +1040,7 @@ void django_views_conf_import() {
import views
urlpatterns = [conf.path('foo', views.foo, name='foo'), conf.path('baz')]
""";
ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(content), "my_package", pythonFile("urls.py"));
assertThat(projectSymbolTable.isDjangoView("views.foo")).isTrue();
}
Expand All @@ -1057,7 +1057,7 @@ def view_method(self):
def get_urlpatterns(self):
return [path("something", self.view_method, name="something")]
""";
ProjectLevelSymbolTable projectSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(content), "my_package", pythonFile("mod.py"));
// SONARPY-2322: should be true
assertThat(projectSymbolTable.isDjangoView("my_package.mod.ClassWithViews.view_method")).isFalse();
Expand Down Expand Up @@ -1086,7 +1086,7 @@ class MetaField: ...
class Field(MetaField()): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));

var descriptors = projectSymbolTable.getDescriptorsFromModule("mod");
Expand All @@ -1111,7 +1111,7 @@ void superclasses_without_descriptor_unresolved_import() {
class Field(MetaField): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbol) projectSymbolTable.getSymbol("mod.Field");
assertThat(symbol.hasUnresolvedTypeHierarchy()).isTrue();
Expand All @@ -1124,7 +1124,7 @@ void class_wth_imported_metaclass() {
class WithMetaclass(metaclass=ABCMeta): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("abc.ABCMeta");
Expand All @@ -1137,7 +1137,7 @@ class LocalMetaClass: ...
class WithMetaclass(metaclass=LocalMetaClass): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("mod.LocalMetaClass");
Expand All @@ -1150,7 +1150,7 @@ void class_wth_unresolved_import_metaclass() {
class WithMetaclass(metaclass=UnresolvedMetaClass): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.metaclassFQN()).isEqualTo("unknown.UnresolvedMetaClass");
Expand All @@ -1163,7 +1163,7 @@ def foo(): ...
class WithMetaclass(metaclass=foo()): ...
""";

var projectSymbolTable = new ProjectLevelSymbolTable();
var projectSymbolTable = empty();
projectSymbolTable.addModule(parseWithoutSymbols(code), "", pythonFile("mod.py"));
var symbol = (ClassSymbolImpl) projectSymbolTable.getSymbol("mod.WithMetaclass");
assertThat(symbol.hasMetaClass()).isTrue();
Expand All @@ -1172,7 +1172,7 @@ class WithMetaclass(metaclass=foo()): ...

@Test
void projectPackages() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = empty();
projectLevelSymbolTable.addProjectPackage("first.package");
projectLevelSymbolTable.addProjectPackage("second.package");
projectLevelSymbolTable.addProjectPackage("third");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void nameConflictBetweenSubmoduleAndInitNameFromStubs2() {

@Test
void importingSubmodulesTest() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

FileInput libTree = parseWithoutSymbols(
"""
Expand Down Expand Up @@ -180,7 +180,7 @@ class A: ...

@Test
void importingRedefinedSubmodulesTest() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

FileInput libTree = parseWithoutSymbols(
"""
Expand Down Expand Up @@ -224,7 +224,7 @@ class A: ...

@Test
void importingRedefinedSubmodules2Test() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

FileInput libTree = parseWithoutSymbols(
"""
Expand Down Expand Up @@ -264,7 +264,7 @@ void resolveStubsWithImportedModuleVariableDescriptor() {

@Test
void importFunctionWithDecorators() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var libTree = parseWithoutSymbols(
"""
def lib_decorator(): ...
Expand All @@ -290,7 +290,7 @@ def foo(): ...

@Test
void importFunctionWithImportedDecorators() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var libTree = parseWithoutSymbols(
"""
def lib_decorator(): ...
Expand Down Expand Up @@ -321,7 +321,7 @@ def foo(): ...

@Test
void importedFunctionDecoratorNamesTest() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var libTree = parseWithoutSymbols(
"""
from abc import abstractmethod
Expand Down Expand Up @@ -353,7 +353,7 @@ def foo(self): ...

@Test
void relativeImports() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
FileInput initTree = parseWithoutSymbols("");
PythonFile initFile = pythonFile("__init__.py");
projectLevelSymbolTable.addModule(initTree, "my_package", initFile);
Expand Down Expand Up @@ -384,7 +384,7 @@ def foo(): ...

@Test
void importFunctionWithUnresolvedImportParameterTypes() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var libTree = parseWithoutSymbols(
"""
def imported_function(params: list[str]): ...
Expand All @@ -406,7 +406,7 @@ def imported_function(params: list[str]): ...

@Test
void classTypeAndAlias() {
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var libTree = parseWithoutSymbols(
"""
class MyClass():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ class A:
b : int
"""
);
var projectLevelSymbolTable = new ProjectLevelSymbolTable();
var projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));

var symbol = (ClassSymbol) projectLevelSymbolTable.getSymbol("mod.A");
Expand Down Expand Up @@ -1268,7 +1268,7 @@ void inferFunctionParameterTypesMultiFile() {
"class A: ...",
"def foo2(p1: dict, p2: A): ..."
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var modFile = pythonFile("mod.py");
projectLevelSymbolTable.addModule(tree, "", modFile);
ProjectLevelTypeTable projectLevelTypeTable = new ProjectLevelTypeTable(projectLevelSymbolTable);
Expand Down Expand Up @@ -2722,7 +2722,7 @@ void type_origin_of_project_function() {
FileInput tree = parseWithoutSymbols(
"def foo() -> int: ..."
);
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
projectLevelSymbolTable.addModule(tree, "", pythonFile("mod.py"));
ProjectLevelTypeTable projectLevelTypeTable = new ProjectLevelTypeTable(projectLevelSymbolTable);

Expand Down Expand Up @@ -3378,7 +3378,7 @@ void wildCardImportsMultiFile() {
def foo(): pass
def bar(): pass
""");
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
var modFile = pythonFile("mod.py");
projectLevelSymbolTable.addModule(tree, "", modFile);
ProjectLevelTypeTable projectLevelTypeTable = new ProjectLevelTypeTable(projectLevelSymbolTable);
Expand Down Expand Up @@ -3549,7 +3549,7 @@ private static Statement lastStatement(StatementList statementList) {
}

private static class TestProject {
private final ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
private final ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

public TestProject addModule(String moduleName, String code) {
FileInput tree = parseWithoutSymbols(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void isTypeWithNameStubNamesTest() {

@Test
void isTypeWithNameProjectNamesTest() {
ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

FileInput tree = parseWithoutSymbols(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class PythonIndexer {

private final Map<URI, String> packageNames = new HashMap<>();
private final PythonParser parser = PythonParser.create();
private final ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable();
private final ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();

public ProjectLevelSymbolTable projectLevelSymbolTable() {
return projectLevelSymbolTable;
Expand Down

0 comments on commit ecff6b5

Please sign in to comment.