Enforce top-level imports via ruff PLC0415#1349
Merged
Merged
Conversation
Adds ``PLC0415`` to ``[tool.ruff.lint]`` select. Function-scoped imports must now opt in with ``# noqa: PLC0415`` plus an inline comment justifying the reason. Catches a pattern that LLM contributors frequently introduce. No opt-outs needed in the tree today; the rule is clean against ``ruff check`` as-is. Mirrors the approach taken in yarl PR aio-libs/yarl#1697.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1349 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 28 28
Lines 3627 3627
Branches 265 265
=======================================
Hits 3622 3622
Misses 3 3
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What do these changes do?
Adds
PLC0415(import-outside-top-level) to[tool.ruff.lint]select alongside the existing
IandUPgroups. Function-scopedimports must now opt in with
# noqa: PLC0415plus a commentexplaining why (typically avoiding a heavy optional dependency at
import time). This catches a pattern that LLM contributors frequently
introduce.
There are no opt-outs needed in the tree today:
ruff check --select PLC0415againstmultidict/,tests/, andbenchmarks/passes as-is. Conditional top-level imports under
if TYPE_CHECKING:/if sys.version_info >= ...:/try / except ImportErrorare not flagged by the rule.Mirrors the approach taken in yarl PR
#1697, which added the
same rule there for the same reason. Following the AGENTS.md
guidance that each new ruff lint group should be its own PR.
Are there changes in behavior for the user?
No, contributor tooling only.
Is it a substantial burden for the maintainers to support this?
No. The rule has zero opt-outs in the current tree. Future function-
scoped imports must be justified inline with
# noqa: PLC0415anda comment, which is the explicit intent.
Related issue number
N/A; mirrors the approach taken in yarl #1697.
Checklist
ruff checkand the pre-commit suite against the tree)CONTRIBUTORS.txt: N/A (no such file in this repo)CHANGES/folder (CHANGES/1349.contrib.rst)Agent run details (optional, for reviewers)
Drafted with Claude Code (Opus 4.7); reviewed by @bdraco.
Verified: