Skip to content

Commit 7872918

Browse files
committed
Python: Fix pytest fixture unused import FPs
1 parent f3a9c9a commit 7872918

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

python/ql/src/Imports/UnusedImport.ql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212

1313
import python
1414
import Variables.Definition
15+
import semmle.python.ApiGraphs
16+
17+
private predicate is_pytest_fixture(Import imp, Variable name) {
18+
exists(Alias a |
19+
a = imp.getAName() and
20+
a.getAsname().(Name).getVariable() = name and
21+
API::moduleImport("pytest")
22+
.getMember("fixture")
23+
.getReturn()
24+
.getAValueReachableFromSource()
25+
.asExpr() = a.getValue()
26+
)
27+
}
1528

1629
predicate global_name_used(Module m, string name) {
1730
exists(Name u, GlobalVariable v |
@@ -117,6 +130,7 @@ predicate unused_import(Import imp, Variable name) {
117130
not all_not_understood(imp.getEnclosingModule()) and
118131
not imported_module_used_in_doctest(imp) and
119132
not imported_alias_used_in_typehint(imp, name) and
133+
not is_pytest_fixture(imp, name) and
120134
// Only consider import statements that actually point-to something (possibly an unknown module).
121135
// If this is not the case, it's likely that the import statement never gets executed.
122136
imp.getAName().getValue().pointsTo(_)

python/ql/test/query-tests/Imports/unused/UnusedImport.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55
| imports_test.py:10:1:10:22 | Import | Import of 'top_level_cycle' is not used. |
66
| imports_test.py:27:1:27:25 | Import | Import of 'func2' is not used. |
77
| imports_test.py:34:1:34:14 | Import | Import of 'module2' is not used. |
8-
| imports_test.py:116:1:116:67 | Import | Import of 'fixture' is not used. |
98
| imports_test.py:116:1:116:67 | Import | Import of 'not_a_fixture' is not used. |
10-
| imports_test.py:116:1:116:67 | Import | Import of 'wrapped_fixture' is not used. |

0 commit comments

Comments
 (0)