test: Remove unused Python imports #12896
Merged
+0
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
%
ruff check --output-format=concise
# https://docs.astral.sh/ruff%
ruff check --fix
%
ruff rule F401
unused-import (F401)
Derived from the Pyflakes linter.
Fix is sometimes available.
What it does
Checks for unused imports.
Why is this bad?
Unused imports add a performance overhead at runtime, and risk creating
import cycles. They also increase the cognitive load of reading the code.
If an import statement is used to check for the availability or existence
of a module, consider using
importlib.util.find_spec
instead.If an import statement is used to re-export a symbol as part of a module's
public interface, consider using a "redundant" import alias, which
instructs Ruff (and other tools) to respect the re-export, and avoid
marking it as unused, as in:
Alternatively, you can use
__all__
to declare a symbol as part of the module'sinterface, as in:
Fix safety
Fixes to remove unused imports are safe, except in
__init__.py
files.Applying fixes to
__init__.py
files is currently in preview. The fix offered depends on thetype of the unused import. Ruff will suggest a safe fix to export first-party imports with
either a redundant alias or, if already present in the file, an
__all__
entry. If multiple__all__
declarations are present, Ruff will not offer a fix. Ruff will suggest an unsafe fixto remove third-party and standard library imports -- the fix is unsafe because the module's
interface changes.
Example
Use instead:
To check the availability of a module, use
importlib.util.find_spec
:Options
lint.ignore-init-module-imports
lint.pyflakes.allowed-unused-imports
References
import
importlib.util.find_spec
Fix
Improve the performance of <class or module or ...>
CHANGES
log.