Skip to content

👷 ci: enforce lazy imports with flake8-lazy - #1088

Merged
gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:add-flake8-lazy
Jun 4, 2026
Merged

gaborbernat merged 1 commit into
pypa:mainfrom
gaborbernat:add-flake8-lazy

Conversation

@gaborbernat

Copy link
Copy Markdown
Contributor

build keeps its command-line startup fast by deferring heavy imports, and each module records which ones in a __lazy_modules__ list. Nothing has been checking those lists, so they drifted out of sync with the actual imports. 🐌

This adds flake8-lazy as a pre-commit hook so the lists are verified on every change. It runs only against src/, since deferred imports matter for the shipped package rather than the test suite. Bringing the current code back into compliance meant declaring the parent packages the hook now expects (packaging, build._compat) in __main__ and giving the tarfile compat shim a __lazy_modules__ entry for pathlib.

The lists are declarative, so there is no runtime behavior change; the value is catching a missing or stale declaration before it ships.

The codebase declares deferred imports in __lazy_modules__ to keep CLI
startup fast, but nothing checked the lists stayed in sync, so they had
drifted. Add henryiii's flake8-lazy pre-commit hook to catch missing or
stale declarations going forward.

Scope it to src/ since deferred imports only matter for the shipped
package, not the test suite. Fixing the existing drift means declaring
the parent packages flake8-lazy now requires (packaging, build._compat)
in __main__ and adding pathlib to the tarfile compat shim.
@gaborbernat
gaborbernat enabled auto-merge (squash) June 4, 2026 14:21
@gaborbernat
gaborbernat merged commit 331dc82 into pypa:main Jun 4, 2026
59 checks passed
gaborbernat pushed a commit to gaborbernat/build that referenced this pull request Jun 4, 2026
Satisfy the flake8-lazy check (merged in pypa#1088) for the imports this
branch adds to __main__ and _util.
gaborbernat pushed a commit to gaborbernat/build that referenced this pull request Jun 4, 2026
Satisfy the flake8-lazy check (merged in pypa#1088) for the imports this
branch adds across __main__, _exceptions, _util and util.
@henryiii

henryiii commented Jun 5, 2026

Copy link
Copy Markdown
Contributor
args: [--apply=list]

would make it autocorrect.

@gaborbernat

Copy link
Copy Markdown
Contributor Author

Not possible today, unfortunately. flake8-lazy --apply always emits double-quoted strings and ignores the project quote style, while this repo formats with ruff set to quote-style = "single". So args: [--apply=list] ping-pongs with ruff-format — flake8-lazy rewrites the list to double quotes, ruff-format rewrites it back to single — and pre-commit never converges (it also single-lines lists under its own --line-length, default 88 vs our 120).

Filed henryiii/flake8-lazy#75 to have --apply respect the project's quote style (or add --quote-style, including a preserve mode). Once that lands I'll switch the hook to autocorrect; keeping it report-only until then.

gaborbernat added a commit that referenced this pull request Jun 6, 2026
* ✨ feat(deps): show wanted vs found in missing-deps error

A --no-isolation build that hit an unmet requirement printed a flat list
that could not distinguish an absent package from one installed at the
wrong version, and never named the interpreter it inspected. Diagnosing
issue #504 required stepping through the code with pdb to learn that a
package the user had installed was simply invisible to the Python running
build.

The error now reports, per requirement, the version specifier wanted
against the version found (or that the package is not installed), and
names the interpreter checked. The public check_dependency and
check_dependencies contracts are unchanged; only the rendering of their
result is new, in build._util.format_unmet_dependencies.

* 🔧 chore: declare new imports as lazy

Satisfy the flake8-lazy check (merged in #1088) for the imports this
branch adds to __main__ and _util.

* 🔧 chore: call it 'Unmet dependencies'

Address review: rename the error header from 'Missing dependencies' to
'Unmet dependencies', a better qualifier for a present-but-wrong-version
case. Updated message, docs and tests.

---------

Co-authored-by: Test <test@test.com>
@gaborbernat
gaborbernat deleted the add-flake8-lazy branch July 10, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants