Skip to content

Commit 67369a4

Browse files
SONARPY-1810 Track types in case of multiple assignments in module scope (#1786)
1 parent 60c51e1 commit 67369a4

20 files changed

+1311
-365
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable
4848
SymbolTableBuilder symbolTableBuilder = packageName != null ? new SymbolTableBuilder(packageName, pythonFile) : new SymbolTableBuilder(pythonFile);
4949
symbolTableBuilder.visitFileInput(rootTree);
5050
var symbolTable = new SymbolTableBuilderV2(rootTree).build();
51-
rootTree.accept(new TypeInferenceV2(new ProjectLevelTypeTable(ProjectLevelSymbolTable.empty()), pythonFile, symbolTable));
51+
new TypeInferenceV2(new ProjectLevelTypeTable(ProjectLevelSymbolTable.empty()), pythonFile, symbolTable).inferTypes(rootTree);
5252
}
5353

5454
public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable File workingDirectory, String packageName,
@@ -60,7 +60,7 @@ public PythonVisitorContext(FileInput rootTree, PythonFile pythonFile, @Nullable
6060

6161
var symbolTable = new SymbolTableBuilderV2(rootTree)
6262
.build();
63-
rootTree.accept(new TypeInferenceV2(new ProjectLevelTypeTable(projectLevelSymbolTable), pythonFile, symbolTable));
63+
new TypeInferenceV2(new ProjectLevelTypeTable(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-
rootTree.accept(new TypeInferenceV2(new ProjectLevelTypeTable(projectLevelSymbolTable), pythonFile, symbolTable));
74+
new TypeInferenceV2(new ProjectLevelTypeTable(projectLevelSymbolTable), pythonFile, symbolTable).inferTypes(rootTree);
7575
}
7676

7777
public PythonVisitorContext(PythonFile pythonFile, RecognitionException parsingException) {

python-frontend/src/main/java/org/sonar/python/semantic/v2/SymbolV2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void addUsage(Name name, UsageV2.Kind kind) {
3939
}
4040
}
4141

42-
boolean hasSingleBindingUsage() {
42+
public boolean hasSingleBindingUsage() {
4343
return usages.stream().filter(UsageV2::isBindingUsage).toList().size() == 1;
4444
}
4545
}

python-frontend/src/main/java/org/sonar/python/semantic/v2/SymbolsModuleTypeProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private PythonType convertToClassType(ClassSymbol symbol, Map<Symbol, PythonType
154154
}
155155

156156
private PythonType convertToUnionType(AmbiguousSymbol ambiguousSymbol, Map<Symbol, PythonType> createdTypesBySymbol) {
157-
List<PythonType> pythonTypes = ambiguousSymbol.alternatives().stream().map(a -> convertToType(a, createdTypesBySymbol)).toList();
157+
Set<PythonType> pythonTypes = ambiguousSymbol.alternatives().stream().map(a -> convertToType(a, createdTypesBySymbol)).collect(Collectors.toSet());
158158
return new UnionType(pythonTypes);
159159
}
160160

0 commit comments

Comments
 (0)