Skip to content

Remove unused imports - March 2025 Edition #131152

Description

@vstinner

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

Activity

  1. added 2 commits that reference this issue on Mar 12, 2025
  2. vstinner commented on Mar 12, 2025

    @vstinner
    MemberAuthor

    See also PR gh-131150.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 12, 2025
  4. added 4 commits that reference this issue on Mar 12, 2025
  5. corona10 commented on Mar 12, 2025

    @corona10
    Member

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

  6. vstinner commented on Mar 12, 2025

    @vstinner
    MemberAuthor

    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.

  7. StanFromIreland commented on Mar 12, 2025

    @StanFromIreland
    Member

    ruff's 401 works great for me. I can open a PR with a few :-)

    edit: I went through all of ruffs suggestions

  8. donbarbos commented on Mar 13, 2025

    @donbarbos
    Contributor

    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)

  9. donbarbos commented on Mar 13, 2025

    @donbarbos
    Contributor

    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

  10. donbarbos commented on Mar 13, 2025

    @donbarbos
    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 :-)

  11. vstinner commented on Mar 13, 2025

    @vstinner
    MemberAuthor

    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.

  12. donbarbos commented on Mar 13, 2025

    @donbarbos
    Contributor

    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)

  13. added 4 commits that reference this issue on Mar 13, 2025
  14. vstinner commented on Mar 13, 2025

    @vstinner
    MemberAuthor

    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:

  15. added 6 commits that reference this issue on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions