Skip to content

chore: enforce top-level imports with ruff PLC0415#20141

Closed
zzstoatzz wants to merge 1 commit intomainfrom
chore/enforce-top-level-imports
Closed

chore: enforce top-level imports with ruff PLC0415#20141
zzstoatzz wants to merge 1 commit intomainfrom
chore/enforce-top-level-imports

Conversation

@zzstoatzz
Copy link
Copy Markdown
Collaborator

@zzstoatzz zzstoatzz commented Jan 7, 2026

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:

  • Added PLC0415 to ruff's extend-select in pyproject.toml with preview = true
  • Moved imports from inside functions to the top of files where possible
  • Added # noqa: PLC0415 only where imports must remain deferred (circular imports)

For AI agents: If you hit the PLC0415 lint 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 check passes
  • Tests pass
  • No circular import errors

🤖 Generated with Claude Code

Copy link
Copy Markdown
Collaborator

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

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

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".

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 7, 2026

Merging this PR will not alter performance

Summary

✅ 2 untouched benchmarks


Comparing chore/enforce-top-level-imports (14abbad) with main (956ecab)

Open in CodSpeed

@zzstoatzz zzstoatzz marked this pull request as draft January 7, 2026 20:24
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.
@zzstoatzz zzstoatzz force-pushed the chore/enforce-top-level-imports branch from 82970a5 to 14abbad Compare January 7, 2026 20:28
@github-actions
Copy link
Copy Markdown
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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 5, 2026

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.

@github-actions github-actions Bot closed this Feb 5, 2026
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.

2 participants