Files
codeql/python/ql/src/Imports/ModuleImportsItself.ql
Taus 603d37cd60 Python: Port ModuleImportsItself.ql
Uses the existing machinery in ImportResolution.qll, after adding a few
convenience predicates.

The new modelling actually manages to find a result that the old
points-to analysis did not. Apart from that there are no test changes.
2026-03-09 17:22:06 +00:00

31 lines
990 B
Plaintext

/**
* @name Module imports itself
* @description A module imports itself
* @kind problem
* @tags quality
* maintainability
* useless-code
* @problem.severity recommendation
* @sub-severity high
* @precision very-high
* @id py/import-own-module
*/
import python
import semmle.python.dataflow.new.DataFlow
private import semmle.python.dataflow.new.internal.ImportResolution
predicate modules_imports_itself(ImportingStmt i, Module m) {
m = i.getEnclosingModule() and
ImportResolution::importedBy(i, m) and
// Exclude `from m import submodule` where the imported member is a submodule of m
not exists(ImportMember im | im = i.(Import).getAName().getValue() |
ImportResolution::getImmediateModuleReference(m).asExpr() = im.getModule() and
ImportResolution::importedBy(i, any(Module sub | sub != m))
)
}
from ImportingStmt i, Module m
where modules_imports_itself(i, m)
select i, "The module '" + ImportResolution::moduleName(m) + "' imports itself."