GeezerVison is showing github.com — Remove unused imports - March 2025 Edition · Issue #131152 · python/cpython
Remove unused imports - March 2025 Edition #131152DescriptionFeature or enhancement
Proposal:
There are a bunch of unused imports in Python stdlib. I'm working on pull requests to remove them.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Activityvstinner commented on Mar 12, 2025 corona10 commented on Mar 12, 2025 Do you plan to add CI for checking unused imports? cc @hugovk vstinner commented on Mar 12, 2025
Do you plan to add CI for checking unused imports? cc @hugovk
I tried ruff check but it failed to detect many unused imports.
So I used pyflakes as usual, but this tool doesn't take # noqa comments in account. I had to filter the output manually to ignore imports known to be used (ex: import side effects) even if they seem to be unused.
Maybe a ruff check can be used, even if it's incomplete. StanFromIreland commented on Mar 12, 2025 ruff's 401 works great for me. I can open a PR with a few :-)
edit: I went through all of ruffs suggestions donbarbos commented on Mar 13, 2025 today i accidentally came across next unused import :-)
|
try: |
|
import readline # noqa: F401 |
|
except ImportError: |
|
pass |
and i'm going to check cli modules from cmdline.rst list (i'll try not to repeat with changes) donbarbos commented on Mar 13, 2025 This command working for me is fine: uvx ruff check ./Lib/ --select F401 --isolated --ignore-noqa
This command found 209 cases only in ./Lib/test/ dir for F401 rule donbarbos commented on Mar 13, 2025 cc @vstinner I can send a PR for each module separately if you are against it.
You have already wrote in description that you are already working on it, that is why I am asking :-) vstinner commented on Mar 13, 2025 import readline # noqa: F401: this import is used. When you import readline, there is a side effect of executing code which makes the command line more usable. That's why there is the # noqa comment: to tell the linter that the import is actually used.
donbarbos commented on Mar 13, 2025
import readline # noqa: F401: this import is used. When you import readline, there is a side effect of executing code which makes the command line more usable. That's why there is the # noqa comment: to tell the linter that the import is actually used.
sorry, I'll remember it (but I guessed there was some side effect) vstinner commented on Mar 13, 2025 I close this issue, since the initial issue (unused imports) has been fixed. If someone wants to automate detecting unused imports, I suggest to open a new issue. But be aware that there are reasons why it's not automated yet.
This command found 209 cases only in ./Lib/test/ dir for F401 rule
Be careful, linters are stupid and reports false alarms. I checked ruff check ./Lib/ --select F401 --isolated output: it reports 160 "errors", but I found a single real error (an unused import, I will leave it for now, it's not a big deal). That's why it's not automated yet, the tooling isn't smart enough to ignore unused imports which are actually used.
I added # noqa in some places, but in some others, some developers were against it so I gave up. See:
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|