Python: Fix pytest fixture unused import FPs

This commit is contained in:
Taus
2024-06-14 12:05:55 +00:00
parent f3a9c9a9dc
commit 78729180ad
2 changed files with 14 additions and 2 deletions

View File

@@ -12,6 +12,19 @@
import python
import Variables.Definition
import semmle.python.ApiGraphs
private predicate is_pytest_fixture(Import imp, Variable name) {
exists(Alias a |
a = imp.getAName() and
a.getAsname().(Name).getVariable() = name and
API::moduleImport("pytest")
.getMember("fixture")
.getReturn()
.getAValueReachableFromSource()
.asExpr() = a.getValue()
)
}
predicate global_name_used(Module m, string name) {
exists(Name u, GlobalVariable v |
@@ -117,6 +130,7 @@ predicate unused_import(Import imp, Variable name) {
not all_not_understood(imp.getEnclosingModule()) and
not imported_module_used_in_doctest(imp) and
not imported_alias_used_in_typehint(imp, name) and
not is_pytest_fixture(imp, name) and
// Only consider import statements that actually point-to something (possibly an unknown module).
// If this is not the case, it's likely that the import statement never gets executed.
imp.getAName().getValue().pointsTo(_)

View File

@@ -5,6 +5,4 @@
| imports_test.py:10:1:10:22 | Import | Import of 'top_level_cycle' is not used. |
| imports_test.py:27:1:27:25 | Import | Import of 'func2' is not used. |
| imports_test.py:34:1:34:14 | Import | Import of 'module2' is not used. |
| imports_test.py:116:1:116:67 | Import | Import of 'fixture' is not used. |
| imports_test.py:116:1:116:67 | Import | Import of 'not_a_fixture' is not used. |
| imports_test.py:116:1:116:67 | Import | Import of 'wrapped_fixture' is not used. |