test: Remove unused Python imports#12896
Merged
wu-sheng merged 2 commits intoapache:masterfrom Dec 23, 2024
Merged
Conversation
74ad480 to
b61c6df
Compare
wu-sheng
approved these changes
Dec 23, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 F401unused-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_specinstead.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__.pyfiles.Applying fixes to
__init__.pyfiles 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-importslint.pyflakes.allowed-unused-importsReferences
importimportlib.util.find_specFix
Improve the performance of <class or module or ...>
CHANGESlog.