Module imports itselfΒΆ
ID: py/import-own-module
Kind: problem
Security severity:
Severity: recommendation
Precision: very-high
Tags:
- quality
- maintainability
- useless-code
Query suites:
- python-code-quality.qls
- python-security-and-quality.qls
Click to see the query in the CodeQL repository
There is no need for a module to import itself. A module importing itself may lead to errors as the module may be in an incomplete state when imported by itself.
RecommendationΒΆ
Remove the import statement. Convert all expressions of the form mod.name where βmodβ is the name of the current module to name.
ExampleΒΆ
In this example the module, ModuleImportsItself imports itself and has an expression referencing the module it is in as well.
import ModuleImportsItself
def factorial(n):
if n <= 0:
return 1
return n * ModuleImportsItself.factorial(n - 1)
The import can be removed and the reference can be corrected.
def factorial(n):
if n <= 0:
return 1
return n * factorial(n - 1)
ReferencesΒΆ
Python: Modules.