Skip to content

Commit 7d8a44b

Browse files
SONARPY-954 Typeshed symbols retrieved by FQN should be consistent (#1034)
1 parent fadb4c7 commit 7d8a44b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

python-frontend/src/main/java/org/sonar/python/types/TypeShed.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public static Symbol symbolWithFQN(String stdLibModuleName, String fullyQualifie
187187
String[] fqnSplittedByDot = fullyQualifiedName.split("\\.");
188188
String symbolLocalNameFromFqn = fqnSplittedByDot[fqnSplittedByDot.length - 1];
189189

190+
// TODO: improve performance - see SONARPY-955
190191
Set<Symbol> matchByName = symbols.stream().filter(s -> symbolLocalNameFromFqn.equals(s.name())).collect(Collectors.toSet());
191192
if (matchByName.size() == 1) {
192193
return matchByName.iterator().next();
@@ -477,19 +478,9 @@ public static Symbol symbolWithFQN(String fullyQualifiedName) {
477478
if (builtinSymbol != null) {
478479
return builtinSymbol;
479480
}
480-
for (Map<String, Symbol> symbolsByFqn : typeShedSymbols.values()) {
481-
Symbol symbol = symbolsByFqn.get(fullyQualifiedName);
482-
if (symbol != null) {
483-
return symbol;
484-
}
485-
}
486481
String[] fqnSplittedByDot = fullyQualifiedName.split("\\.");
487482
String moduleName = Arrays.stream(fqnSplittedByDot, 0, fqnSplittedByDot.length - 1).collect(Collectors.joining("."));
488-
Set<Symbol> symbols = symbolsForModule(moduleName);
489-
if (!symbols.isEmpty()) {
490-
return typeShedSymbols.get(moduleName).get(fullyQualifiedName);
491-
}
492-
return null;
483+
return symbolWithFQN(moduleName, fullyQualifiedName);
493484
}
494485

495486
public static String normalizedFqn(String fqn) {

python-frontend/src/test/java/org/sonar/python/types/TypeShedTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,16 @@ public void pythonVersions() {
448448
setPythonVersions(PythonVersionUtils.allVersions());
449449
}
450450

451+
@Test
452+
public void symbolWithFQN_should_be_consistent() {
453+
// smtplib imports typing.Sequence only in Python3, hence typing.Sequence has kind CLASS
454+
TypeShed.symbolsForModule("smtplib");
455+
Symbol sequence = TypeShed.symbolWithFQN("typing.Sequence");
456+
assertThat(sequence.kind()).isEqualTo(Kind.AMBIGUOUS);
457+
Map<String, Symbol> typing = TypeShed.symbolsForModule("typing").stream().collect(Collectors.toMap(Symbol::name, Function.identity()));
458+
assertThat(sequence).isSameAs(typing.get("Sequence"));
459+
}
460+
451461
private static SymbolsProtos.ModuleSymbol moduleSymbol(String protobuf) throws TextFormat.ParseException {
452462
SymbolsProtos.ModuleSymbol.Builder builder = SymbolsProtos.ModuleSymbol.newBuilder();
453463
TextFormat.merge(protobuf, builder);

0 commit comments

Comments
 (0)