Skip to content

Commit bb37bde

Browse files
SONARPY-972 Remove Typeshed parsing logic
1 parent 84bbe0e commit bb37bde

File tree

9 files changed

+10
-396
lines changed

9 files changed

+10
-396
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ public class ClassSymbolImpl extends SymbolImpl implements ClassSymbol {
7373
public ClassSymbolImpl(ClassDef classDef, @Nullable String fullyQualifiedName, PythonFile pythonFile) {
7474
super(classDef.name().name(), fullyQualifiedName);
7575
this.setKind(Kind.CLASS);
76-
String fileId = null;
77-
if (!SymbolUtils.isTypeShedFile(pythonFile)) {
78-
Path path = pathOf(pythonFile);
79-
fileId = path != null ? path.toString() : pythonFile.toString();
80-
}
76+
Path path = pathOf(pythonFile);
77+
String fileId = path != null ? path.toString() : pythonFile.toString();
8178
hasDecorators = !classDef.decorators().isEmpty();
8279
classDefinitionLocation = locationInFile(classDef.name(), fileId);
8380
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.sonar.python.types.TypeShed;
4747
import org.sonar.python.types.protobuf.SymbolsProtos;
4848

49-
import static org.sonar.python.semantic.SymbolUtils.isTypeShedFile;
5049
import static org.sonar.python.semantic.SymbolUtils.pathOf;
5150
import static org.sonar.python.tree.TreeUtils.locationInFile;
5251
import static org.sonar.python.types.InferredTypes.anyType;
@@ -78,12 +77,8 @@ public class FunctionSymbolImpl extends SymbolImpl implements FunctionSymbol {
7877
isAsynchronous = functionDef.asyncKeyword() != null;
7978
hasDecorators = !functionDef.decorators().isEmpty();
8079
decorators = decorators(functionDef);
81-
String fileId = null;
82-
isStub = isTypeShedFile(pythonFile);
83-
if (!isStub) {
84-
Path path = pathOf(pythonFile);
85-
fileId = path != null ? path.toString() : pythonFile.toString();
86-
}
80+
isStub = false;
81+
String fileId = Optional.ofNullable(pathOf(pythonFile)).map(Path::toString).orElse(pythonFile.toString());
8782
functionDefinitionLocation = locationInFile(functionDef.name(), fileId);
8883
}
8984

@@ -338,10 +333,6 @@ public ParameterType(InferredType inferredType, boolean isKeywordVariadic, boole
338333
}
339334
}
340335

341-
public void setAnnotatedReturnTypeName(@Nullable TypeAnnotation returnTypeAnnotation) {
342-
annotatedReturnTypeName = annotatedTypeName(returnTypeAnnotation);
343-
}
344-
345336
private String annotatedTypeName(@Nullable TypeAnnotation typeAnnotation) {
346337
return Optional.ofNullable(typeAnnotation)
347338
.map(TypeAnnotation::expression)

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
import org.sonar.python.types.TypeShed;
8888

8989
import static org.sonar.python.semantic.SymbolUtils.boundNamesFromExpression;
90-
import static org.sonar.python.semantic.SymbolUtils.isTypeShedFile;
9190
import static org.sonar.python.semantic.SymbolUtils.resolveTypeHierarchy;
9291

9392
// SymbolTable based on https://docs.python.org/3/reference/executionmodel.html#naming-and-binding
@@ -99,7 +98,6 @@ public class SymbolTableBuilder extends BaseTreeVisitor {
9998
private FileInput fileInput = null;
10099
private Set<Tree> assignmentLeftHandSides = new HashSet<>();
101100
private final PythonFile pythonFile;
102-
private static final List<String> BASE_MODULES = Arrays.asList("", "typing", "typing_extensions");
103101

104102
public SymbolTableBuilder(PythonFile pythonFile) {
105103
fullyQualifiedModuleName = null;
@@ -132,9 +130,7 @@ public void visitFileInput(FileInput fileInput) {
132130
createAmbiguousSymbols();
133131
addSymbolsToTree((FileInputImpl) fileInput);
134132
fileInput.accept(new ThirdPhaseVisitor());
135-
if (!isTypeShedFile(pythonFile)) {
136-
TypeInference.inferTypes(fileInput, pythonFile);
137-
}
133+
TypeInference.inferTypes(fileInput, pythonFile);
138134
}
139135

140136
private static class SymbolToUpdate {
@@ -273,11 +269,9 @@ public void visitFileInput(FileInput tree) {
273269
createScope(tree, null);
274270
enterScope(tree);
275271
moduleScope = currentScope();
276-
if (!SymbolUtils.isTypeShedFile(pythonFile) || !BASE_MODULES.contains(pythonFile.fileName())) {
277-
Map<String, Symbol> typeShedSymbols = TypeShed.builtinSymbols();
278-
for (String name : BuiltinSymbols.all()) {
279-
currentScope().createBuiltinSymbol(name, typeShedSymbols);
280-
}
272+
Map<String, Symbol> typeShedSymbols = TypeShed.builtinSymbols();
273+
for (String name : BuiltinSymbols.all()) {
274+
currentScope().createBuiltinSymbol(name, typeShedSymbols);
281275
}
282276
super.visitFileInput(tree);
283277
}
@@ -684,7 +678,7 @@ public void visitFunctionDef(FunctionDef functionDef) {
684678
functionSymbolImpl.setParametersWithType(parameters);
685679
}
686680
TypeAnnotation typeAnnotation = functionDef.returnTypeAnnotation();
687-
if (typeAnnotation != null && !isTypeShedFile(pythonFile)) {
681+
if (typeAnnotation != null) {
688682
functionSymbolImpl.setDeclaredReturnType(InferredTypes.fromTypeAnnotation(typeAnnotation));
689683
}
690684
}

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.sonar.plugins.python.api.tree.UnpackingExpression;
5858
import org.sonar.python.tree.TreeUtils;
5959
import org.sonar.python.types.TypeShed;
60-
import org.sonar.python.types.TypeShedPythonFile;
6160

6261
import static org.sonar.plugins.python.api.symbols.Symbol.Kind.CLASS;
6362
import static org.sonar.plugins.python.api.symbols.Symbol.Kind.FUNCTION;
@@ -90,11 +89,6 @@ static void resolveTypeHierarchy(ClassDef classDef, @Nullable Symbol symbol, Pyt
9089
return;
9190
}
9291
ClassSymbolImpl classSymbol = (ClassSymbolImpl) symbol;
93-
if (isBuiltinTypeshedFile(pythonFile) && "str".equals(classSymbol.fullyQualifiedName())) {
94-
classSymbol.addSuperClass(symbolsByName.get("object"));
95-
classSymbol.addSuperClass(symbolsByName.get("Sequence"));
96-
return;
97-
}
9892
ArgList argList = classDef.args();
9993
if (argList == null) {
10094
return;
@@ -126,36 +120,10 @@ private static void addParentClass(PythonFile pythonFile, Map<String, Symbol> sy
126120
if ("typing.Generic".equals(argumentSymbol.fullyQualifiedName())) {
127121
classSymbol.setSupportsGenerics(true);
128122
}
129-
Symbol normalizedArgumentSymbol = normalizeSymbol(argumentSymbol, pythonFile, symbolsByName);
130-
if (normalizedArgumentSymbol != null) {
131-
classSymbol.addSuperClass(normalizedArgumentSymbol);
132-
}
123+
classSymbol.addSuperClass(argumentSymbol);
133124
}
134125
}
135126

136-
/**
137-
* Hardcoding some 'typing' module symbols to avoid incomplete type hierarchy for type 'str'
138-
*/
139-
@CheckForNull
140-
private static Symbol normalizeSymbol(Symbol symbol, PythonFile pythonFile, Map<String, Symbol> symbolsByName) {
141-
if (isTypeShedFile(pythonFile) && (symbol.name().equals("Protocol") || symbol.name().equals("Generic"))) {
142-
// ignore Protocol and Generic to avoid having incomplete type hierarchies
143-
return null;
144-
}
145-
if (isTypingFile(pythonFile) && symbol.name().equals("_Collection")) {
146-
return symbolsByName.get("Collection");
147-
}
148-
return symbol;
149-
}
150-
151-
private static boolean isBuiltinTypeshedFile(PythonFile pythonFile) {
152-
return isTypeShedFile(pythonFile) && pythonFile.fileName().isEmpty();
153-
}
154-
155-
private static boolean isTypingFile(PythonFile pythonFile) {
156-
return isTypeShedFile(pythonFile) && pythonFile.fileName().equals("typing");
157-
}
158-
159127
@CheckForNull
160128
private static Symbol getSymbolFromArgument(RegularArgument regularArgument) {
161129
Expression expression = regularArgument.expression();
@@ -222,10 +190,6 @@ public static Path pathOf(PythonFile pythonFile) {
222190
}
223191
}
224192

225-
public static boolean isTypeShedFile(PythonFile pythonFile) {
226-
return pythonFile instanceof TypeShedPythonFile;
227-
}
228-
229193
/**
230194
* @return the offset between parameter position and argument position:
231195
* 0 if there is no implicit first parameter (self, cls, etc...)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public class TypeShed {
6666
private static final Map<String, Set<Symbol>> builtinGlobalSymbols = new HashMap<>();
6767
private static final Set<String> modulesInProgress = new HashSet<>();
6868

69-
private static final String THIRD_PARTY_2AND3 = "typeshed/third_party/2and3/";
70-
private static final String THIRD_PARTY_2 = "typeshed/third_party/2/";
71-
private static final String THIRD_PARTY_3 = "typeshed/third_party/3/";
72-
private static final String CUSTOM_THIRD_PARTY = "custom/";
7369
private static final String PROTOBUF_CUSTOM_STUBS = "custom_protobuf/";
7470
private static final String PROTOBUF = "stdlib_protobuf/";
7571
private static final String PROTOBUF_THIRD_PARTY = "third_party_protobuf/";

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)