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 — gh-120417: Add more #noqa to used imports in the stdlib by vstinner · Pull Request #120454 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Jun 13, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Tools such as ruff can ignore "imported but unused" warnings if a line ends with "# noqa: F401". It avoids the temptation to remove an import which is used effectively.

  • Issue: Remove unused imports from the stdlib #120417

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-120417: Add more #noqa to used imports in the stdlib
fc384b1
Tools such as ruff can ignore "imported but unused" warnings if a
line ends with "# noqa: F401". It avoids the temptation to remove
an import which is used effectively.
@vstinner vstinner added the skip news label Jun 13, 2024
@vstinner
vstinner requested review from brettcannon, ericsnowcurrently, gpshead, ncoghlan, rhettinger and warsaw as code owners June 13, 2024 14:23
@bedevere-app bedevere-app Bot mentioned this pull request Jun 13, 2024
Remove unused imports from the stdlib #120417
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 13, 2024
@vstinner vstinner mentioned this pull request Jun 13, 2024
gh-120417: Add #noqa to used imports in the stdlib #120421
Merged
@vstinner

vstinner commented Jun 13, 2024

Copy link
Copy Markdown
Member Author

cc @AlexWaygood @gpshead @serhiy-storchaka

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AlexWaygood

AlexWaygood commented Jun 13, 2024

Copy link
Copy Markdown
Member

I'm still against this approach, except for maybe Lib/collections/abc.py, where there are only two being noqa'd. If we need to ignore this many imports in a file, there are lots of other workarounds:

  • Add __all__ to the file so it's clear to the linter what's being deliberately re-exported
  • Ignore the whole file by putting # noqa: F401 on a line by itself near the top of the file
  • Use a --exclude CLI flag when running the linter to exclude the whole file from the check.

In my opinion, this is just too ugly for a file like ctypes/__init__.py.

serhiy-storchaka and gpshead reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Jun 13, 2024

Copy link
Copy Markdown
Member

tl;dr I strongly favor using __all__ for nearly all of these cases.

In a very few of the cases I'd say the linters aren't able to do a good job of identifying false positives for F401. However, IMHO the linter is actually doing the right thing in the remaining cases. In that vast majority it is simply exposing the fact that we aren't using __all__ when we should be. If we used __all__ the way we should then there would be no need for #noqa, etc. in those cases.

Here we're dealing with a whole lot of bound objects that are meant to be available to users as part of a module's API. __all__ is exactly the place where you indicate which bound objects are available to users. 1 All objects that are imported just to be exposed as-is (I usually call them "aliases") fall into this category. __all__ is the natural place to identify them, to effectively say "these objects were imported to be exposed by this module."

So why are we avoiding __all__ for all these "alias" cases?

Footnotes

  1. I'd argue that having __all__ is a good idea generally; it makes the module's public API explicit to users/maintainers and to the runtime, available on the module for code to easily look at. ↩

AlexWaygood and gpshead reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 13, 2024

Copy link
Copy Markdown
Member

This is pretty common idiom -- defining the names in the submodules and exposing them as a public API at the package level.

Ideally, linters should be aware of this idiom.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 13, 2024

Copy link
Copy Markdown
Member Author

Here is a different approach to fix these warnings: #120461 PR adding __all__ to modules. I left ctypes and ssl aside for now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead

gpshead commented Jun 13, 2024

Copy link
Copy Markdown
Member

Thanks Eric, agreed, I like the ultimate explicitness of __all__. (I was primarily intending to point out that we should introduce it with that specific intent in my earlier comment)

(as for improvements to static analysis like linters, lets just file those as FRs in their respective trackers - ruff is still young and lacks some practical things)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger
rhettinger removed their request for review June 14, 2024 17:29
@vstinner

vstinner commented Jun 14, 2024

Copy link
Copy Markdown
Member Author

I closed this PR: #120461 looks like a better approach.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this Jun 14, 2024
@vstinner
vstinner deleted the import_qa2 branch June 14, 2024 17:54
@vstinner vstinner mentioned this pull request Mar 13, 2025
Remove unused imports - March 2025 Edition #131152
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead Awaiting requested review from gpshead gpshead is a code owner
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@vstinner @AlexWaygood @ericsnowcurrently @serhiy-storchaka @gpshead
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.