You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove unused imports - March 2025 Edition #131152
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.
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
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 :-)
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.
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)
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:
Activity
gh-131152, pkgutil: Remove unused imports (#131149)
gh-131152: Remove unused imports in `_pyrepl` and `ctypes` (#131150)
vstinner commentedon Mar 12, 2025
See also PR gh-131150.
pythongh-131152: Remove unused imports from tests
pythongh-131152: Remove unused imports from tests (part 2)
pythongh-131152: Remove unused imports from tools
pythongh-131152: Remove unused imports from cases_generator
corona10 commentedon Mar 12, 2025
Do you plan to add CI for checking unused imports? cc @hugovk
vstinner commentedon Mar 12, 2025
I tried
ruff checkbut it failed to detect many unused imports.So I used
pyflakesas usual, but this tool doesn't take# noqacomments 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
ruffcheck can be used, even if it's incomplete.StanFromIreland commentedon 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 commentedon Mar 13, 2025
today i accidentally came across next unused import :-)
cpython/Lib/code.py
Lines 378 to 381 in f7ac656
and i'm going to check cli modules from
cmdline.rstlist (i'll try not to repeat with changes)donbarbos commentedon Mar 13, 2025
This command working for me is fine:
uvx ruff check ./Lib/ --select F401 --isolated --ignore-noqaThis command found 209 cases only in
./Lib/test/dir for F401 ruledonbarbos commentedon 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 commentedon Mar 13, 2025
import readline # noqa: F401: this import is used. When you importreadline, there is a side effect of executing code which makes the command line more usable. That's why there is the# noqacomment: to tell the linter that the import is actually used.donbarbos commentedon Mar 13, 2025
sorry, I'll remember it (but I guessed there was some side effect)
gh-131152: Remove unused imports from tests (#131153)
gh-131152: Remove unused imports from tests (part 2) (#131154)
gh-131152: Remove unused imports from `Tools/{build,wasm}` (#131155)
gh-131152: Remove unused imports from `Tools/cases_generator` (#131156)
vstinner commentedon 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.
Be careful, linters are stupid and reports false alarms. I checked
ruff check ./Lib/ --select F401 --isolatedoutput: 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
# noqain some places, but in some others, some developers were against it so I gave up. See:pythongh-131152, pkgutil: Remove unused imports (python#131149)
pythongh-131152: Remove unused imports in `_pyrepl` and `ctypes` (pyt…
pythongh-131152: Remove unused imports from tests (python#131153)
pythongh-131152: Remove unused imports from tests (part 2) (python#13…
pythongh-131152: Remove unused imports from `Tools/{build,wasm}` (pyt…
pythongh-131152: Remove unused imports from `Tools/cases_generator` (p…