-
Notifications
You must be signed in to change notification settings - Fork 103
SONARPY-944 Use precomputed Typeshed symbols for third-party libraries in the Python analyzer #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75f1353
5d5306b
369dd7f
90ef511
e46e978
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,6 @@ | |
| import static org.sonar.plugins.python.api.types.BuiltinTypes.NONE_TYPE; | ||
| import static org.sonar.plugins.python.api.types.BuiltinTypes.STR; | ||
| import static org.sonar.plugins.python.api.types.BuiltinTypes.TUPLE; | ||
| import static org.sonar.python.types.TypeShedThirdParties.commonSymbols; | ||
| import static org.sonar.python.types.TypeShedThirdParties.getModuleSymbols; | ||
|
|
||
| public class TypeShed { | ||
|
|
@@ -73,6 +72,7 @@ public class TypeShed { | |
| private static final String THIRD_PARTY_3 = "typeshed/third_party/3/"; | ||
| private static final String CUSTOM_THIRD_PARTY = "custom/"; | ||
| private static final String PROTOBUF = "protobuf/"; | ||
| private static final String PROTOBUF_THIRD_PARTY = "protobuf/stubs/"; | ||
| private static final String BUILTINS_FQN = "builtins"; | ||
| private static final String BUILTINS_PREFIX = BUILTINS_FQN + "."; | ||
| // Those fundamentals builtins symbols need not to be ambiguous for the frontend to work properly | ||
|
|
@@ -96,7 +96,7 @@ private TypeShed() { | |
| public static Map<String, Symbol> builtinSymbols() { | ||
| if ((TypeShed.builtins == null)) { | ||
| supportedPythonVersions = ProjectPythonVersion.currentVersions().stream().map(PythonVersionUtils.Version::serializedValue).collect(Collectors.toSet()); | ||
| Map<String, Symbol> builtins = getSymbolsFromProtobufModule(BUILTINS_FQN); | ||
| Map<String, Symbol> builtins = getSymbolsFromProtobufModule(BUILTINS_FQN, false); | ||
| builtins.put(NONE_TYPE, new ClassSymbolImpl(NONE_TYPE, NONE_TYPE)); | ||
| TypeShed.builtins = Collections.unmodifiableMap(builtins); | ||
| TypeShed.builtinGlobalSymbols.put("", new HashSet<>(builtins.values())); | ||
|
|
@@ -271,16 +271,13 @@ private static Map<String, Symbol> searchTypeShedForModule(String moduleName) { | |
| modulesInProgress.remove(moduleName); | ||
| return customSymbols; | ||
| } | ||
| Map<String, Symbol> symbolsFromProtobuf = getSymbolsFromProtobufModule(moduleName); | ||
| Map<String, Symbol> symbolsFromProtobuf = getSymbolsFromProtobufModule(moduleName, false); | ||
| if (!symbolsFromProtobuf.isEmpty()) { | ||
| modulesInProgress.remove(moduleName); | ||
| return symbolsFromProtobuf; | ||
| } | ||
| Map<String, Symbol> thirdPartySymbols = getModuleSymbols(moduleName, THIRD_PARTY_2AND3, builtinGlobalSymbols); | ||
| if (thirdPartySymbols.isEmpty()) { | ||
| thirdPartySymbols = commonSymbols(getModuleSymbols(moduleName, THIRD_PARTY_2, builtinGlobalSymbols), | ||
| getModuleSymbols(moduleName, THIRD_PARTY_3, builtinGlobalSymbols), moduleName); | ||
| } | ||
|
|
||
| Map<String, Symbol> thirdPartySymbols = getSymbolsFromProtobufModule(moduleName, true); | ||
| modulesInProgress.remove(moduleName); | ||
| return thirdPartySymbols; | ||
| } | ||
|
|
@@ -311,8 +308,9 @@ private static boolean isAmbiguousSymbolOfClasses(Symbol symbol) { | |
| return false; | ||
| } | ||
|
|
||
| private static Map<String, Symbol> getSymbolsFromProtobufModule(String moduleName) { | ||
| InputStream resource = TypeShed.class.getResourceAsStream(PROTOBUF + moduleName + ".protobuf"); | ||
| private static Map<String, Symbol> getSymbolsFromProtobufModule(String moduleName, boolean isThirdParty) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a boolean, what about passing the directory name directly (so that this generalizes more easily with custom stubs) ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, I will let you do that in your PR :) |
||
| String protobufDir = isThirdParty ? PROTOBUF_THIRD_PARTY : PROTOBUF; | ||
| InputStream resource = TypeShed.class.getResourceAsStream(protobufDir + moduleName + ".protobuf"); | ||
| if (resource == null) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we have stdlib under
protobufand third parties underprotobuf/stubs. All of this is undertypeswhich currently holdsTypeshedas well.What about going for a
protobuf_stdlib,protobuf_thid_partiesandprotobuf_customall at the same level, for better clarity?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
works for me! if that's fine I will let you also do that in your PR or I can do it in another separate PR