Skip to content

Remove unused imports - March 2025 Edition #131152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vstinner opened this issue Mar 12, 2025 · 10 comments
Closed

Remove unused imports - March 2025 Edition #131152

vstinner opened this issue Mar 12, 2025 · 10 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Mar 12, 2025

Feature 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

@vstinner
Copy link
Member Author

vstinner commented Mar 12, 2025

See also PR gh-131150.

@corona10
Copy link
Member

Do you plan to add CI for checking unused imports? cc @hugovk

@vstinner
Copy link
Member Author

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
Copy link
Contributor

StanFromIreland commented 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
Copy link
Contributor

donBarbos commented Mar 13, 2025

today i accidentally came across next unused import :-)

cpython/Lib/code.py

Lines 378 to 381 in f7ac656

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
Copy link
Contributor

donBarbos commented 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
Copy link
Contributor

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
Copy link
Member Author

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
Copy link
Contributor

donBarbos commented 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
Copy link
Member Author

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:

plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants