Python: Only track modules that are imported

This commit is contained in:
Taus
2021-04-06 19:55:43 +00:00
committed by GitHub
parent 8e11abca40
commit b44db460f6
2 changed files with 18 additions and 5 deletions

View File

@@ -201,6 +201,20 @@ private string moduleNameFromBase(Container file) {
file instanceof File and result = file.getStem()
}
/**
* Holds if `file` may be transitively imported from a file that may serve as the entry point of
* the execution.
*/
private predicate transitively_imported_from_entry_point(File file) {
file.getExtension().matches("%py%") and
exists(File importer |
importer.getParent() = file.getParent() and
exists(ImportExpr i | i.getLocation().getFile() = importer and i.getName() = file.getStem())
|
importer.maybeExecutedDirectly() or transitively_imported_from_entry_point(importer)
)
}
string moduleNameFromFile(Container file) {
exists(string basename |
basename = moduleNameFromBase(file) and
@@ -208,9 +222,10 @@ string moduleNameFromFile(Container file) {
|
result = moduleNameFromFile(file.getParent()) + "." + basename
or
// If execution can start in the folder containing this module, then we will assume `file` can
// be imported as an absolute import, and hence return `basename` as a possible name.
file.getParent().(Folder).mayContainEntryPoint() and result = basename
// If `file` is a transitive import of a file that's executed directly, we allow references
// to it by its `basename`.
transitively_imported_from_entry_point(file) and
result = basename
)
or
isPotentialSourcePackage(file) and