Skip to content

gh-151428: Remove unused imports from stdlib#151478

Merged
hugovk merged 3 commits into
python:mainfrom
hugovk:3.16-rm-unused-imports
Jun 16, 2026
Merged

gh-151428: Remove unused imports from stdlib#151478
hugovk merged 3 commits into
python:mainfrom
hugovk:3.16-rm-unused-imports

Conversation

@hugovk

@hugovk hugovk commented Jun 14, 2026

Copy link
Copy Markdown
Member

Follow on from #151440 and #151442.

Also add Ruff F401 to help avoid this in the future, and exclude any files with false positives (rather than adding noqa comments).

Comment thread .pre-commit-config.yaml

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Pablo might need to update his "Attracts linker errors" line to "Attracts linker errors and unused imports," I think I see a pattern in the affected files ;-)

@johnslavik

johnslavik commented Jun 15, 2026

Copy link
Copy Markdown
Member

Pablo might need to update his "Attracts linker errors" line to "Attracts linker errors and unused imports," I think I see a pattern in the affected files ;-)

Attracts lin[kt]er errors

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I only reviewed the changes to the config files)

Comment thread Tools/.ruff.toml Outdated
Comment on lines +3 to +4
[per-file-target-version]
"jit/example_trace_dump.py" = "py312" # Reused-quote f-strings (PEP 701)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be the target version for the whole jit directory, rather than just one file...?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, updated.

Comment thread Tools/.ruff.toml
Comment on lines +11 to +14
[lint.per-file-ignores]
"c-analyzer/c_parser/__init__.py" = ["F401"] # Re-exports from submodules
"c-analyzer/c_parser/preprocessor/__init__.py" = ["F401"] # Re-exports from submodules
"c-analyzer/c_parser/preprocessor/common.py" = ["F401"] # Re-exported by preprocessor/__init__.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding __all__ and listing the re-exports in __all__ would probably be a more idiomatic way of fixing these errors (but perhaps that's something for a followup PR, since there's a chance it could be controversial :-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will open a followup for this.

Comment thread Lib/.ruff.toml

[lint.per-file-ignores]
"ctypes/__init__.py" = ["F401"] # Re-exports from _ctypes
"ensurepip/__init__.py" = ["F401"] # `import zlib` availability check

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a case like this where there's only one error, I'd usually lean towards using a noqa comment in the code rather than per-file-ignores. But, again, perhaps that's best done as a followup..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for this.

@pablogsal

Copy link
Copy Markdown
Member

LGTM

Pablo might need to update his "Attracts linker errors" line to "Attracts linker errors and unused imports," I think I see a pattern in the affected files ;-)

Keeping those linters busy man 😉

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread Tools/.ruff.toml
[lint]
select = [
"F401", # Unused import
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed to repeat it? It's already in the parent (project) configuration, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent config doesn't define a select, so defaults to ["E4", "E7", "E9", "F"].

We don't want to run the whole F category, or those other ones, so this select overwrites that one.

(There is an extend-select that is in addition to select, but we do want a smaller set here.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I mixed Lib/.ruff.toml and .ruff.toml. You're right.

@hugovk hugovk merged commit 627adb5 into python:main Jun 16, 2026
57 checks passed
@hugovk hugovk deleted the 3.16-rm-unused-imports branch June 16, 2026 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants