Skip to content

Commit a2ea1e1

Browse files
SONARPY-1889 use a copy of ProjectLevelSymbolTable for v2 type inference
1 parent c161b75 commit a2ea1e1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

python-frontend/src/main/java/org/sonar/plugins/python/api/PythonVisitorContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable
6060

6161
var symbolTable = new SymbolTableBuilderV2(rootTree)
6262
.build();
63-
new TypeInferenceV2(new ProjectLevelTypeTable(projectLevelSymbolTable), pythonFile, symbolTable).inferTypes(rootTree);
63+
new TypeInferenceV2(new ProjectLevelTypeTable(ProjectLevelSymbolTable.from(projectLevelSymbolTable)), pythonFile, symbolTable).inferTypes(rootTree);
6464
}
6565

6666
public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable File workingDirectory, String packageName,
@@ -71,7 +71,7 @@ public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable
7171
new SymbolTableBuilder(packageName, pythonFile, projectLevelSymbolTable).visitFileInput(rootTree);
7272
var symbolTable = new SymbolTableBuilderV2(rootTree)
7373
.build();
74-
new TypeInferenceV2(new ProjectLevelTypeTable(projectLevelSymbolTable), pythonFile, symbolTable).inferTypes(rootTree);
74+
new TypeInferenceV2(new ProjectLevelTypeTable(ProjectLevelSymbolTable.from(projectLevelSymbolTable)), pythonFile, symbolTable).inferTypes(rootTree);
7575
}
7676

7777
public PythonVisitorContext(PythonFile pythonFile, RecognitionException parsingException) {

python-frontend/src/main/java/org/sonar/python/semantic/ProjectLevelSymbolTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashMap;
2525
import java.util.HashSet;
2626
import java.util.Map;
27+
import java.util.Optional;
2728
import java.util.Set;
2829
import java.util.function.Function;
2930
import java.util.stream.Collectors;
@@ -60,6 +61,16 @@ public static ProjectLevelSymbolTable from(Map<String, Set<Symbol>> globalSymbol
6061
return new ProjectLevelSymbolTable(globalSymbolsByModuleName);
6162
}
6263

64+
public static ProjectLevelSymbolTable from(ProjectLevelSymbolTable from) {
65+
var to = empty();
66+
to.globalDescriptorsByModuleName.putAll(from.globalDescriptorsByModuleName);
67+
Optional.ofNullable(from.globalDescriptorsByFQN)
68+
.ifPresent(v -> to.globalDescriptorsByFQN = new HashMap<>(v));
69+
to.djangoViewsFQN.addAll(from.djangoViewsFQN);
70+
to.importsByModule.putAll(from.importsByModule);
71+
return to;
72+
}
73+
6374
public ProjectLevelSymbolTable() {
6475
this.globalDescriptorsByModuleName = new HashMap<>();
6576
}

0 commit comments

Comments
 (0)