chore: enforce top-level imports with ruff PLC0415#20141
Closed
chore: enforce top-level imports with ruff PLC0415#20141
Conversation
chrisguidry
reviewed
Jan 7, 2026
Collaborator
chrisguidry
left a comment
There was a problem hiding this comment.
I wholeheartedly endorse this strategy, but I also think we should tell the LLMs what it means and what to do about it instead. An agent may just slap that # noqa comment every time it tries to do this. Something like "If you fail the PLC0415 lint rule, hoist the import to the top of the file. Only a human is allowed to approve inline imports by adding the #noqa tag".
enable the import-outside-top-level lint rule to catch deferred imports. existing legitimate uses (circular imports, optional deps) are marked with # noqa: PLC0415. this creates an "electric fence" - new deferred imports will fail the lint unless explicitly marked, while documenting all existing exceptions.
82970a5 to
14abbad
Compare
Contributor
|
This pull request is stale because it has been open 14 days with no activity. To keep this pull request open remove stale label or comment. |
Contributor
|
This pull request was closed because it has been stale for 14 days with no activity. If this pull request is important or you have more to add feel free to re-open it. |
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.
Summary
Enable the
import-outside-top-level(PLC0415) lint rule to enforce top-level imports.This creates an "electric fence" - new deferred imports will fail CI, forcing developers to either move imports to the top level or get explicit human approval for genuinely necessary deferred imports (circular imports, optional dependencies).
Changes:
PLC0415to ruff'sextend-selectin pyproject.toml withpreview = true# noqa: PLC0415only where imports must remain deferred (circular imports)For AI agents: If you hit the
PLC0415lint error, move the import to the top of the file. Do NOT add# noqa: PLC0415- only a human can approve inline imports for circular import avoidance or optional dependencies.Test plan
ruff checkpasses🤖 Generated with Claude Code