Skip to content

Commit 0e91396

Browse files
Typeshed#stubFilesSymbols should not contain null symbols for ambigous third party symbols (#1049)
1 parent 189e8dc commit 0e91396

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ public static Collection<Symbol> stubFilesSymbols() {
165165
Set<Symbol> symbols = new HashSet<>(TypeShed.builtinSymbols().values());
166166
for (Map<String, Symbol> symbolsByFqn : typeShedSymbols.values()) {
167167
for (Symbol symbol : symbolsByFqn.values()) {
168-
symbols.add(isAmbiguousSymbolOfClasses(symbol) ? disambiguateWithLatestPythonSymbol(((AmbiguousSymbol) symbol).alternatives()) : symbol);
168+
Symbol stubSymbol = symbol;
169+
if (isAmbiguousSymbolOfClasses(symbol)) {
170+
Symbol disambiguatedSymbol = disambiguateWithLatestPythonSymbol(((AmbiguousSymbol) symbol).alternatives());
171+
if (disambiguatedSymbol != null) {
172+
stubSymbol = disambiguatedSymbol;
173+
}
174+
}
175+
symbols.add(stubSymbol);
169176
}
170177
}
171178
return symbols;
@@ -283,6 +290,7 @@ private static Map<String, Symbol> searchTypeShedForModule(String moduleName) {
283290
* This method sort ambiguous symbol by python version and returns the one which is valid for
284291
* the most recent Python version.
285292
*/
293+
@CheckForNull
286294
static Symbol disambiguateWithLatestPythonSymbol(Set<Symbol> alternatives) {
287295
int max = Integer.MIN_VALUE;
288296
Symbol latestPythonSymbol = null;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,12 @@ public void typeshed_private_modules_should_not_affect_fqn() {
528528
assertThat(socket.declaredMembers()).extracting(Symbol::name, Symbol::fullyQualifiedName).contains(tuple("connect", "socket.socket.connect"));
529529
assertThat(socket.superClasses()).extracting(Symbol::fullyQualifiedName).containsExactly("object");
530530
}
531+
532+
@Test
533+
public void stubFilesSymbols_third_party_symbols_should_not_be_null() {
534+
// six modules contain ambiguous symbols that only contain class symbols
535+
// however third party symbols don't have validForPythonVersions field set
536+
symbolsForModule("six");
537+
assertThat(TypeShed.stubFilesSymbols()).doesNotContainNull();
538+
}
531539
}

0 commit comments

Comments
 (0)