GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove unused imports - March 2025 Edition · Issue #131152 · python/cpython

Remove unused imports - March 2025 Edition #131152

New issue
Copy link
New issue
Copy link
Closed
Closed
Remove unused imports - March 2025 Edition#131152
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Mar 12, 2025
Issue body actions

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

  • gh-131152, pkgutil: Remove unused imports #131149
  • 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
  • gh-131152: Remove unused imports in tests #131169
Reacted by Cody Maloney

Activity

  1. vstinner
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Mar 12, 2025
  2. vstinner
    added 2 commits that reference this issue on Mar 12, 2025

    gh-131152, pkgutil: Remove unused imports (#131149)

    14aed5e

    gh-131152: Remove unused imports in `_pyrepl` and `ctypes` (#131150)

    db27aee
  3. vstinner commented on Mar 12, 2025

    @vstinner
    vstinner
    on Mar 12, 2025
    MemberAuthor
    More actions

    See also PR gh-131150.

  4. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Mar 12, 2025
  5. vstinner
    added 4 commits that reference this issue on Mar 12, 2025

    pythongh-131152: Remove unused imports from tests

    26bcbb5

    pythongh-131152: Remove unused imports from tests (part 2)

    0c5e97d

    pythongh-131152: Remove unused imports from tools

    76be694

    pythongh-131152: Remove unused imports from cases_generator

    fafc893
  6. corona10 commented on Mar 12, 2025

    @corona10
    corona10
    on Mar 12, 2025
    Member
    More actions

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

  7. vstinner commented on Mar 12, 2025

    @vstinner
    vstinner
    on Mar 12, 2025
    MemberAuthor
    More actions

    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.

  8. StanFromIreland commented on Mar 12, 2025

    @StanFromIreland
    StanFromIreland
    on Mar 12, 2025
    Member
    More actions

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

    edit: I went through all of ruffs suggestions

  9. donbarbos commented on Mar 13, 2025

    @donbarbos
    donbarbos
    on Mar 13, 2025
    Contributor
    More actions

    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)

  10. donbarbos commented on Mar 13, 2025

    @donbarbos
    donbarbos
    on Mar 13, 2025
    Contributor
    More actions

    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

  11. donbarbos commented on Mar 13, 2025

    @donbarbos
    donbarbos
    on Mar 13, 2025
    Contributor
    More actions

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

  12. vstinner commented on Mar 13, 2025

    @vstinner
    vstinner
    on Mar 13, 2025
    MemberAuthor
    More actions

    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.

  13. donbarbos commented on Mar 13, 2025

    @donbarbos
    donbarbos
    on Mar 13, 2025
    Contributor
    More actions

    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)

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

    gh-131152: Remove unused imports from tests (#131153)

    73ab9e2

    gh-131152: Remove unused imports from tests (part 2) (#131154)

    b2ca268

    gh-131152: Remove unused imports from `Tools/{build,wasm}` (#131155)

    85cabed

    gh-131152: Remove unused imports from `Tools/cases_generator` (#131156)

    fcb750a
  15. vstinner commented on Mar 13, 2025

    @vstinner
    vstinner
    on Mar 13, 2025
    MemberAuthor
    More actions

    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:

    • gh-120417: Add #noqa to used imports in the stdlib #120421 (merged)
    • gh-120417: Add #noqa: F401 to tests #120627 (merged)
    • gh-120417: Add more #noqa to used imports in the stdlib #120454 (rejected)
    • gh-120417: Add #noqa to load_tests() in tests #120626 (rejected)
    • Remove unused imports from the stdlib #120417 (discussion)
  16. vstinner
    closed this as completedon Mar 13, 2025
  17. plashchynski
    added 6 commits that reference this issue on Mar 17, 2025

    pythongh-131152, pkgutil: Remove unused imports (python#131149)

    cc34d68

    pythongh-131152: Remove unused imports in `_pyrepl` and `ctypes` (pyt…

    a251b56

    pythongh-131152: Remove unused imports from tests (python#131153)

    0350e1c

    pythongh-131152: Remove unused imports from tests (part 2) (python#13…

    0cc71bc

    pythongh-131152: Remove unused imports from `Tools/{build,wasm}` (pyt…

    7cce36d

    pythongh-131152: Remove unused imports from `Tools/cases_generator` (p…

    ad4f9c8
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/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.