Skip to content

chore: use a custom lazy-exclude-modules list#18

Open
mayeut wants to merge 1 commit into
henryiii:henryiii/chore/fasthelpfrom
mayeut:lazy-config2
Open

chore: use a custom lazy-exclude-modules list#18
mayeut wants to merge 1 commit into
henryiii:henryiii/chore/fasthelpfrom
mayeut:lazy-config2

Conversation

@mayeut

@mayeut mayeut commented May 30, 2026

Copy link
Copy Markdown

as mentioned in pypa#2797 (review), if it's deemed too noisy with the default configuration, maybe at least a part of this could be re-used.

Summary by Sourcery

Introduce tooling to automatically maintain a custom lazy-import exclusion list and align existing lazy module declarations with it.

Enhancements:

  • Add a nox session to generate and update a shared lazy modules exclusion list and apply it across the codebase.
  • Trim per-module lazy_modules sets to rely on the centralized exclusion list for common standard-library and utility modules.

Build:

  • Add a .flake8 configuration file defining lazy-exclude-modules for flake8-lazy.
  • Wire a pre-commit hook to run the new nox session so lazy-exclude-modules stays in sync during development.

@sourcery-ai

sourcery-ai Bot commented May 30, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces an automated workflow (via nox and pre-commit) to generate and enforce a custom flake8-lazy exclude-modules list, and updates the package’s lazy_modules definitions and configuration accordingly.

Sequence diagram for the new lazy modules exclusion update workflow

sequenceDiagram
    actor Dev
    participant pre_commit
    participant nox
    participant update_lazy_modules
    participant cibuildwheel
    participant flake8_lazy
    participant ruff_prek

    Dev->>pre_commit: git commit
    pre_commit->>nox: run hook lazy
    nox->>update_lazy_modules: run session update_lazy_modules

    update_lazy_modules->>cibuildwheel: session.run python -v -m cibuildwheel --help
    update_lazy_modules-->>update_lazy_modules: parse lazy modules from output
    update_lazy_modules-->>update_lazy_modules: update .flake8 lazy-exclude-modules

    alt config_changed or --force
        update_lazy_modules->>flake8_lazy: session.run uvx flake8-lazy --apply set --lazy-exclude-modules
        opt --force
            update_lazy_modules->>ruff_prek: session.run uvx prek run --all-files ruff-check ruff-format
        end
    end

    alt config_changed
        update_lazy_modules->>nox: session.error lazy modules exclude list changed
    end
Loading

File-Level Changes

Change Details Files
Add a nox session to derive and apply a canonical lazy-exclude-modules list and optionally re-run formatting/linting.
  • Introduce update_lazy_modules nox session that runs cibuildwheel under PYTHON_LAZY_IMPORTS, parses verbose import output, filters platform-specific and private modules, and computes a sorted exclusion list.
  • Write the generated exclusion list into a new .flake8 config file and, when changed or when forced, run flake8-lazy --apply set with the computed lazy-exclude-modules across cibuildwheel/*.py except Android resources.
  • When forced, invoke ruff via prek over all files to re-apply checks/formatting, and fail the nox session if the exclusion list changed to surface diffs in CI.
noxfile.py
.flake8
Integrate the lazy modules exclusion check into pre-commit so it runs before ruff and keeps lazy_modules in sync.
  • Add a local pre-commit hook named "lazy" that depends on nox and runs nox -s update_lazy_modules without passing filenames.
  • Ensure this hook is ordered before the ruff pre-commit hooks so lazy-module fixes are applied prior to ruff checks.
.pre-commit-config.yaml
Align lazy_modules sets with the generated exclusion list by trimming previously enumerated modules.
  • Reduce the lazy_modules set in cibuildwheel.main to a smaller, curated subset, dropping various stdlib and internal modules now covered by the generated list.
  • Similarly simplify lazy_modules sets in platform- and feature-specific modules (macos, oci_container, audit) by removing entries that are now part of the global lazy-exclude-modules configuration.
  • Apply mechanical updates across remaining cibuildwheel modules so their lazy-import configuration matches what flake8-lazy derives from the new workflow.
cibuildwheel/__main__.py
cibuildwheel/platforms/macos.py
cibuildwheel/oci_container.py
cibuildwheel/audit.py
cibuildwheel/architecture.py
cibuildwheel/ci.py
cibuildwheel/errors.py
cibuildwheel/extra.py
cibuildwheel/frontend.py
cibuildwheel/logger.py
cibuildwheel/options.py
cibuildwheel/platforms/android.py
cibuildwheel/platforms/ios.py
cibuildwheel/platforms/linux.py
cibuildwheel/platforms/pyodide.py
cibuildwheel/platforms/windows.py
cibuildwheel/projectfiles.py
cibuildwheel/resources/install_certifi.py
cibuildwheel/resources/ios-support/_cross_venv.py
cibuildwheel/resources/ios-support/make_cross_venv.py
cibuildwheel/resources/testing_temp_dir_file.py
cibuildwheel/schema.py
cibuildwheel/util/cmd.py
cibuildwheel/util/file.py
cibuildwheel/util/helpers.py
cibuildwheel/util/packaging.py
cibuildwheel/util/python_build_standalone.py
cibuildwheel/util/resources.py
cibuildwheel/venv.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • The update_lazy_modules session rewrites .flake8 with only the lazy-exclude-modules option, which will discard any existing flake8 configuration; consider merging into an existing config (or a dedicated include file/section) rather than replacing it entirely.
  • The update_lazy_modules logic depends on parsing python -v output and the # destroy prefix, which is fairly brittle against CPython changes; adding a sanity check (e.g. failing if no or very few modules are detected) or isolating this parsing into a helper with clear assumptions would make it safer to maintain.
  • The pre-commit lazy hook always runs nox -s update_lazy_modules, which will be relatively heavy and may fail on environments without Python 3.15; you might want to guard the session against missing interpreters or make the hook opt-in for contributors who need to update the lazy module list.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `update_lazy_modules` session rewrites `.flake8` with only the `lazy-exclude-modules` option, which will discard any existing flake8 configuration; consider merging into an existing config (or a dedicated include file/section) rather than replacing it entirely.
- The `update_lazy_modules` logic depends on parsing `python -v` output and the `# destroy ` prefix, which is fairly brittle against CPython changes; adding a sanity check (e.g. failing if no or very few modules are detected) or isolating this parsing into a helper with clear assumptions would make it safer to maintain.
- The pre-commit `lazy` hook always runs `nox -s update_lazy_modules`, which will be relatively heavy and may fail on environments without Python 3.15; you might want to guard the session against missing interpreters or make the hook opt-in for contributors who need to update the lazy module list.

## Individual Comments

### Comment 1
<location path="noxfile.py" line_range="208" />
<code_context>
     session.run("python", "-m", "build")


+@nox.session(default=False, reuse_venv=True, python="3.15")
+def update_lazy_modules(session: nox.Session) -> None:
+    """
</code_context>
<issue_to_address>
**issue (bug_risk):** Using `python="3.15"` may make this session unusable on many environments until 3.15 is widely available.

Hard-coding `python="3.15"` will cause this session to fail on machines without that interpreter, which will be common for some time. Unless you truly require 3.15-only behavior, consider targeting a more widely available version (e.g., your minimum supported or CI default) or omitting the `python` argument so the session can run with the local default interpreter.
</issue_to_address>

### Comment 2
<location path="noxfile.py" line_range="230-232" />
<code_context>
+        "posixpath",  # POSIX (Linux/macOS)
+    }
+    for line in output.splitlines():
+        destroy = "# destroy "
+        if line.startswith(destroy):
+            name = line[len(destroy) :]
+            extern_private = name.startswith("_") or ("._" in name and "cibuildwheel" not in name)
+            if extern_private or name in platform_specific:
</code_context>
<issue_to_address>
**issue (bug_risk):** Parsing `python -v` output via `"# destroy "` is tightly coupled to CPython’s current verbose import format.

This relies on CPython’s verbose import debug format, which is not a stable API and may change between versions, potentially yielding an incorrect lazy-module list without obvious failure. Please consider adding a version guard, validating that we actually collect modules (and failing clearly if not), or encapsulating this parsing so it’s easier to update if the format changes.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread noxfile.py Outdated
session.run("python", "-m", "build")


@nox.session(default=False, reuse_venv=True, python="3.15")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Using python="3.15" may make this session unusable on many environments until 3.15 is widely available.

Hard-coding python="3.15" will cause this session to fail on machines without that interpreter, which will be common for some time. Unless you truly require 3.15-only behavior, consider targeting a more widely available version (e.g., your minimum supported or CI default) or omitting the python argument so the session can run with the local default interpreter.

Comment thread noxfile.py Outdated
Comment on lines +230 to +232
destroy = "# destroy "
if line.startswith(destroy):
name = line[len(destroy) :]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Parsing python -v output via "# destroy " is tightly coupled to CPython’s current verbose import format.

This relies on CPython’s verbose import debug format, which is not a stable API and may change between versions, potentially yielding an incorrect lazy-module list without obvious failure. Please consider adding a version guard, validating that we actually collect modules (and failing clearly if not), or encapsulating this parsing so it’s easier to update if the format changes.

@henryiii henryiii force-pushed the henryiii/chore/fasthelp branch 2 times, most recently from 899fb7a to 8ff54de Compare June 1, 2026 22:32
@mayeut mayeut force-pushed the lazy-config2 branch 2 times, most recently from a22493a to a658c51 Compare June 8, 2026 05:38
@henryiii henryiii force-pushed the henryiii/chore/fasthelp branch from 8ff54de to cd79b86 Compare June 10, 2026 01:42
the list can be updated using `nox -s update_lazy_modules`
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.

1 participant