Skip to content

Add support for lazy import parsing#23755

Merged
charliermarsh merged 1 commit intomainfrom
charlie/lazy
Mar 6, 2026
Merged

Add support for lazy import parsing#23755
charliermarsh merged 1 commit intomainfrom
charlie/lazy

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

@charliermarsh charliermarsh commented Mar 6, 2026

Summary

This PR adds is_lazy to the AST and parser, and supports lazy import formatting in the formatter. The name is_lazy matches the CPython AST, though we could modify it if there's demand.

There are no further changes to Ruff rules or even to Ruff import sorting, since those deserve separate design discussions.

I've also omitted support for flagging semantic syntax errors (e.g., lazy import inside def).

See: #21305.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 6, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.07%. The percentage of expected errors that received a diagnostic held steady at 77.62%.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 6, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 6, 2026

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 59 diagnostics
+ Found 60 diagnostics

@charliermarsh charliermarsh force-pushed the charlie/lazy branch 2 times, most recently from ca3e429 to 487b7c5 Compare March 6, 2026 02:27
@charliermarsh charliermarsh marked this pull request as ready for review March 6, 2026 02:33
@astral-sh-bot astral-sh-bot bot requested a review from amyreese March 6, 2026 02:33
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 6, 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.

@carljm carljm removed their request for review March 6, 2026 02:52
@charliermarsh charliermarsh added the parser Related to the parser label Mar 6, 2026
@charliermarsh charliermarsh marked this pull request as draft March 6, 2026 03:04
@charliermarsh charliermarsh marked this pull request as ready for review March 6, 2026 03:09
Copy link
Copy Markdown
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

I've also omitted support for flagging semantic syntax errors (e.g., lazy import inside def).

👍

@charliermarsh charliermarsh merged commit 8a10b67 into main Mar 6, 2026
51 checks passed
@charliermarsh charliermarsh deleted the charlie/lazy branch March 6, 2026 14:12
@dylwil3
Copy link
Copy Markdown
Collaborator

dylwil3 commented Mar 6, 2026

Probably fine for now, but this causes isort to give a diagnostic for every lazy import and to give a fix that breaks the semantics

echo "lazy import os" | cargo run -p ruff --  check --no-cache --isolated --select I --output-format concise  --fix -
import os
Found 1 error (1 fixed, 0 remaining).

@charliermarsh
Copy link
Copy Markdown
Member Author

Oh nice thank you, I will fix that.

charliermarsh added a commit that referenced this pull request Mar 6, 2026
## Summary

We now reject cases like:

- `lazy import ...` inside functions, including `async def`
- `lazy from ... import ...` inside functions, including `async def`
- `lazy import ...` and `lazy from ... import ...` inside class bodies
- `lazy import ...` and `lazy from ... import ...` anywhere inside a
`try` statement, including `except` / `except*`
- `lazy from ... import *` anywhere
- `lazy from __future__ import ...` anywhere

A follow-up to #23755.

See: #21305.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants