-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUnintentionalImport.ql
More file actions
32 lines (28 loc) · 997 Bytes
/
UnintentionalImport.ql
File metadata and controls
32 lines (28 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @name 'import *' may pollute namespace
* @description Importing a module using 'import *' may unintentionally pollute the global
* namespace if the module does not define '__all__'
* @kind problem
* @tags maintainability
* modularity
* @problem.severity recommendation
* @sub-severity high
* @precision very-high
* @id py/polluting-import
*/
import python
predicate import_star(ImportStar imp, ModuleValue exporter) {
exporter.importedAs(imp.getImportedModuleName())
}
predicate all_defined(ModuleValue exporter) {
exporter.isBuiltin()
or
exporter.getScope().(ImportTimeScope).definesName("__all__")
or
exporter.getScope().getInitModule().(ImportTimeScope).definesName("__all__")
}
from ImportStar imp, ModuleValue exporter
where import_star(imp, exporter) and not all_defined(exporter)
select imp,
"Import pollutes the enclosing namespace, as the imported module $@ does not define '__all__'.",
exporter, exporter.getName()