Python: Fix 'unused import' to no longer give alerts for imported modules used in doctests.

This commit is contained in:
Mark Shannon
2019-01-18 11:08:53 +00:00
parent f147b63bb8
commit 9f93bf8d17
4 changed files with 29 additions and 0 deletions

View File

@@ -41,6 +41,22 @@ predicate all_not_understood(Module m) {
)
}
predicate imported_module_used_in_doctest(Import imp) {
exists(string modname |
((Name)imp.getAName().getAsname()).getId() = modname
and
/* Look for doctests containing the patterns:
* >>> …name…
* ... …name…
*/
exists(StrConst doc |
doc.getEnclosingModule() = imp.getScope() and
doc.isDocString() and
doc.getText().regexpMatch("[\\s\\S]*(>>>|\\.\\.\\.).*" + modname + "[\\s\\S]*")
)
)
}
predicate unused_import(Import imp, Variable name) {
((Name)imp.getAName().getAsname()).getVariable() = name
and
@@ -65,6 +81,8 @@ predicate unused_import(Import imp, Variable name) {
and
/* Assume that opaque `__all__` includes imported module */
not all_not_understood(imp.getEnclosingModule())
and
not imported_module_used_in_doctest(imp)
}

View File

@@ -57,3 +57,4 @@ string repr(Expr e) {
else
result = e.toString()
}