Skip to content

Allow users to ban lazy imports#23847

Merged
charliermarsh merged 1 commit intomainfrom
charlie/lazy-import-ii
Mar 10, 2026
Merged

Allow users to ban lazy imports#23847
charliermarsh merged 1 commit intomainfrom
charlie/lazy-import-ii

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

This PR renames TID254 to lazy-import-mismatch and expands it into a single rule with two complementary settings: require-lazy and ban-lazy. Both settings accept "all", list[str], or { include = ..., exclude = [...] }, and we validate that the two selectors don’t overlap.

This covers the two main use-cases users asked for:

  • Require lazy imports by default, except for known side-effectful modules. Example:

    require-lazy = { include = "all", exclude = ["sitecustomize"] }

    This enforces lazy imports everywhere Ruff can rewrite them, while leaving sitecustomize eager.

  • Forbid lazy imports for modules that must stay eager, or even forbid lazy imports by default with a small allowlist.

    # Require `sitecustomize` to stay eager.
    ban-lazy = ["sitecustomize"]
    
    # Ban lazy imports everywhere except `typing`.
    ban-lazy = { include = "all", exclude = ["typing"] }

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Mar 9, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 10, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh force-pushed the charlie/lazy-import-ii branch from b13344a to 87d3185 Compare March 10, 2026 00:07
@charliermarsh charliermarsh marked this pull request as ready for review March 10, 2026 00:10
@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 10, 2026 00:10
@charliermarsh charliermarsh merged commit e46e458 into main Mar 10, 2026
43 checks passed
@charliermarsh charliermarsh deleted the charlie/lazy-import-ii branch March 10, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants