Skip to content

Commit

Permalink
SONARPY-2261 get rid of TypeInferenceV2.typesBySymbol field (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim-grebeniuk-sonarsource authored Oct 25, 2024
1 parent 3e383e9 commit 6c6cddb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -50,19 +49,18 @@ public class TypeInferenceV2 {
private final ProjectLevelTypeTable projectLevelTypeTable;
private final SymbolTable symbolTable;
private final PythonFile pythonFile;
private Map<SymbolV2, Set<PythonType>> typesBySymbol = new HashMap<>();

public TypeInferenceV2(ProjectLevelTypeTable projectLevelTypeTable, PythonFile pythonFile, SymbolTable symbolTable) {
this.projectLevelTypeTable = projectLevelTypeTable;
this.symbolTable = symbolTable;
this.pythonFile = pythonFile;
}

public void inferTypes(FileInput fileInput) {
public Map<SymbolV2, Set<PythonType>> inferTypes(FileInput fileInput) {
TrivialTypeInferenceVisitor trivialTypeInferenceVisitor = new TrivialTypeInferenceVisitor(projectLevelTypeTable, pythonFile);
fileInput.accept(trivialTypeInferenceVisitor);

inferTypesAndMemberAccessSymbols(fileInput);
var typesBySymbol = inferTypesAndMemberAccessSymbols(fileInput);

fileInput.accept(new BaseTreeVisitor() {
@Override
Expand All @@ -71,20 +69,17 @@ public void visitFunctionDef(FunctionDef funcDef) {
inferTypesAndMemberAccessSymbols(funcDef);
}
});
}

public Map<SymbolV2, Set<PythonType>> getTypesBySymbol() {
return typesBySymbol;
}

private void inferTypesAndMemberAccessSymbols(FileInput fileInput) {
private Map<SymbolV2, Set<PythonType>> inferTypesAndMemberAccessSymbols(FileInput fileInput) {
StatementList statements = fileInput.statements();
if (statements == null) {
return;
return Map.of();
}
var moduleSymbols = symbolTable.getSymbolsByRootTree(fileInput);

typesBySymbol = inferTypesAndMemberAccessSymbols(
return inferTypesAndMemberAccessSymbols(
fileInput,
statements,
moduleSymbols,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2888,8 +2888,7 @@ private static Map<SymbolV2, Set<PythonType>> inferTypesBySymbol(String lines) {
FileInput root = parse(lines);
var symbolTable = new SymbolTableBuilderV2(root).build();
var typeInferenceV2 = new TypeInferenceV2(PROJECT_LEVEL_TYPE_TABLE, pythonFile, symbolTable);
typeInferenceV2.inferTypes(root);
return typeInferenceV2.getTypesBySymbol();
return typeInferenceV2.inferTypes(root);
}

private static FileInput inferTypes(String lines) {
Expand Down

0 comments on commit 6c6cddb

Please sign in to comment.