From f7fd565fa565211ddb6bae74c979b0c2ea13ed72 Mon Sep 17 00:00:00 2001 From: mayeut Date: Mon, 8 Jun 2026 07:24:36 +0200 Subject: [PATCH] chore: use a custom lazy-exclude-modules list the list can be updated using `nox -s update_lazy_modules` --- cibuildwheel/__main__.py | 13 ------ cibuildwheel/architecture.py | 2 +- cibuildwheel/audit.py | 4 -- cibuildwheel/ci.py | 2 - cibuildwheel/errors.py | 2 - cibuildwheel/extra.py | 2 +- cibuildwheel/frontend.py | 2 +- cibuildwheel/logger.py | 12 +---- cibuildwheel/oci_container.py | 7 --- cibuildwheel/options.py | 13 ------ cibuildwheel/platforms/android.py | 10 ---- cibuildwheel/platforms/ios.py | 8 ---- cibuildwheel/platforms/linux.py | 10 ---- cibuildwheel/platforms/macos.py | 7 --- cibuildwheel/platforms/pyodide.py | 8 ---- cibuildwheel/platforms/windows.py | 9 ---- cibuildwheel/projectfiles.py | 2 +- cibuildwheel/schema.py | 2 +- cibuildwheel/util/cmd.py | 2 +- cibuildwheel/util/file.py | 13 +----- cibuildwheel/util/helpers.py | 2 +- cibuildwheel/util/packaging.py | 8 +--- cibuildwheel/util/python_build_standalone.py | 1 - cibuildwheel/util/resources.py | 2 - cibuildwheel/venv.py | 5 -- noxfile.py | 49 ++++++++++++++++++++ pyproject.toml | 6 +++ 27 files changed, 65 insertions(+), 138 deletions(-) diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index c5d5d7723..10134e542 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -1,27 +1,14 @@ from __future__ import annotations __lazy_modules__ = { - "argparse", "cibuildwheel._compat", "cibuildwheel._compat.tarfile", - "cibuildwheel.architecture", "cibuildwheel.ci", "cibuildwheel.logger", - "cibuildwheel.options", "cibuildwheel.platforms", - "cibuildwheel.selector", "cibuildwheel.typing", - "cibuildwheel.util", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.resources", - "contextlib", - "functools", - "io", - "pathlib", - "shutil", "tempfile", - "textwrap", "traceback", } diff --git a/cibuildwheel/architecture.py b/cibuildwheel/architecture.py index 4fe437410..fb2f1a113 100644 --- a/cibuildwheel/architecture.py +++ b/cibuildwheel/architecture.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"platform", "re", "shutil", "subprocess"} +__lazy_modules__ = {"platform", "subprocess"} import platform as platform_module import re diff --git a/cibuildwheel/audit.py b/cibuildwheel/audit.py index dadaef04c..37674a8f6 100644 --- a/cibuildwheel/audit.py +++ b/cibuildwheel/audit.py @@ -2,12 +2,8 @@ __lazy_modules__ = { "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.cmd", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.venv", - "pathlib", "subprocess", } diff --git a/cibuildwheel/ci.py b/cibuildwheel/ci.py index 09619f127..648f29c2d 100644 --- a/cibuildwheel/ci.py +++ b/cibuildwheel/ci.py @@ -1,5 +1,3 @@ -__lazy_modules__ = {"cibuildwheel.util", "cibuildwheel.util.helpers"} - import os import re from enum import Enum diff --git a/cibuildwheel/errors.py b/cibuildwheel/errors.py index b3fd2a6ed..dee96d97e 100644 --- a/cibuildwheel/errors.py +++ b/cibuildwheel/errors.py @@ -4,8 +4,6 @@ semantically clear and unique. """ -__lazy_modules__ = {"textwrap"} - import textwrap diff --git a/cibuildwheel/extra.py b/cibuildwheel/extra.py index 9b1abcb1c..0fbec59b9 100644 --- a/cibuildwheel/extra.py +++ b/cibuildwheel/extra.py @@ -4,7 +4,7 @@ from __future__ import annotations -__lazy_modules__ = {"io", "json", "urllib", "urllib.error", "urllib.request"} +__lazy_modules__ = {"json", "urllib", "urllib.error", "urllib.request"} import json import time diff --git a/cibuildwheel/frontend.py b/cibuildwheel/frontend.py index 41c53b341..475ce8643 100644 --- a/cibuildwheel/frontend.py +++ b/cibuildwheel/frontend.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"cibuildwheel.util", "cibuildwheel.util.helpers", "shlex"} +__lazy_modules__ = {"shlex"} import dataclasses import shlex diff --git a/cibuildwheel/logger.py b/cibuildwheel/logger.py index 69314fac9..2408d48f5 100644 --- a/cibuildwheel/logger.py +++ b/cibuildwheel/logger.py @@ -1,16 +1,6 @@ from __future__ import annotations -__lazy_modules__ = { - "cibuildwheel.ci", - "contextlib", - "functools", - "hashlib", - "humanize", - "io", - "pathlib", - "re", - "textwrap", -} +__lazy_modules__ = {"cibuildwheel.ci", "hashlib", "humanize"} import codecs import contextlib diff --git a/cibuildwheel/oci_container.py b/cibuildwheel/oci_container.py index 49e14a93c..d9c18b8ae 100644 --- a/cibuildwheel/oci_container.py +++ b/cibuildwheel/oci_container.py @@ -2,19 +2,12 @@ __lazy_modules__ = { "cibuildwheel.ci", - "cibuildwheel.errors", "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.cmd", - "cibuildwheel.util.helpers", - "contextlib", - "io", "json", "platform", "shlex", - "shutil", "subprocess", - "textwrap", "uuid", } diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index 2d3e32a33..977ead8db 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -1,27 +1,14 @@ from __future__ import annotations __lazy_modules__ = { - "cibuildwheel.architecture", - "cibuildwheel.environment", - "cibuildwheel.frontend", "cibuildwheel.logger", - "cibuildwheel.oci_container", "cibuildwheel.projectfiles", - "cibuildwheel.selector", "cibuildwheel.typing", - "cibuildwheel.util", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", - "collections", "configparser", - "contextlib", "difflib", "packaging", "packaging.specifiers", - "pathlib", "shlex", - "textwrap", - "tomllib", } import collections diff --git a/cibuildwheel/platforms/android.py b/cibuildwheel/platforms/android.py index 4a3e11fef..1dd2653cf 100644 --- a/cibuildwheel/platforms/android.py +++ b/cibuildwheel/platforms/android.py @@ -3,29 +3,19 @@ __lazy_modules__ = { "build", "build.env", - "cibuildwheel.architecture", "cibuildwheel.audit", - "cibuildwheel.frontend", "cibuildwheel.logger", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.util.python_build_standalone", "cibuildwheel.venv", "filelock", "packaging", "packaging.utils", - "pathlib", "platform", "pprint", - "re", - "runpy", "shlex", - "shutil", "subprocess", - "textwrap", - "typing", } import os diff --git a/cibuildwheel/platforms/ios.py b/cibuildwheel/platforms/ios.py index bf896d8f2..c761a2300 100644 --- a/cibuildwheel/platforms/ios.py +++ b/cibuildwheel/platforms/ios.py @@ -2,25 +2,17 @@ __lazy_modules__ = { "cibuildwheel.audit", - "cibuildwheel.frontend", "cibuildwheel.logger", "cibuildwheel.platforms.macos", - "cibuildwheel.util", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.venv", "filelock", "packaging", "packaging.version", - "pathlib", "platform", "shlex", - "shutil", "subprocess", - "textwrap", - "typing", } import dataclasses diff --git a/cibuildwheel/platforms/linux.py b/cibuildwheel/platforms/linux.py index 32fd841fb..596d46d8a 100644 --- a/cibuildwheel/platforms/linux.py +++ b/cibuildwheel/platforms/linux.py @@ -2,19 +2,9 @@ __lazy_modules__ = { "cibuildwheel.audit", - "cibuildwheel.frontend", "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", - "collections", - "contextlib", - "pathlib", - "shutil", "subprocess", - "textwrap", - "typing", } import contextlib diff --git a/cibuildwheel/platforms/macos.py b/cibuildwheel/platforms/macos.py index 00f347151..762142913 100644 --- a/cibuildwheel/platforms/macos.py +++ b/cibuildwheel/platforms/macos.py @@ -3,22 +3,15 @@ __lazy_modules__ = { "cibuildwheel.audit", "cibuildwheel.ci", - "cibuildwheel.frontend", "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.venv", "filelock", "inspect", "packaging", "packaging.version", - "pathlib", "platform", - "re", - "shutil", "subprocess", } diff --git a/cibuildwheel/platforms/pyodide.py b/cibuildwheel/platforms/pyodide.py index 6afe0a264..9b70416d8 100644 --- a/cibuildwheel/platforms/pyodide.py +++ b/cibuildwheel/platforms/pyodide.py @@ -1,24 +1,16 @@ from __future__ import annotations __lazy_modules__ = { - "cibuildwheel.architecture", "cibuildwheel.audit", - "cibuildwheel.frontend", "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.util.python_build_standalone", "cibuildwheel.venv", "filelock", "json", - "pathlib", - "shutil", "subprocess", "tempfile", - "tomllib", } import dataclasses diff --git a/cibuildwheel/platforms/windows.py b/cibuildwheel/platforms/windows.py index 545847277..7379eada1 100644 --- a/cibuildwheel/platforms/windows.py +++ b/cibuildwheel/platforms/windows.py @@ -1,23 +1,14 @@ from __future__ import annotations __lazy_modules__ = { - "cibuildwheel.architecture", "cibuildwheel.audit", - "cibuildwheel.frontend", "cibuildwheel.logger", - "cibuildwheel.util", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "cibuildwheel.util.helpers", - "cibuildwheel.util.packaging", "cibuildwheel.venv", "filelock", - "pathlib", "platform", - "shutil", "subprocess", - "textwrap", - "typing", } import dataclasses diff --git a/cibuildwheel/projectfiles.py b/cibuildwheel/projectfiles.py index ecc6a2130..cf597c581 100644 --- a/cibuildwheel/projectfiles.py +++ b/cibuildwheel/projectfiles.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"configparser", "contextlib", "dependency_groups"} +__lazy_modules__ = {"configparser", "dependency_groups"} import ast import configparser diff --git a/cibuildwheel/schema.py b/cibuildwheel/schema.py index b48793fd8..377f9c0f6 100644 --- a/cibuildwheel/schema.py +++ b/cibuildwheel/schema.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"cibuildwheel.util", "json"} +__lazy_modules__ = {"json"} import json diff --git a/cibuildwheel/util/cmd.py b/cibuildwheel/util/cmd.py index 1795eb8e2..2f0290dee 100644 --- a/cibuildwheel/util/cmd.py +++ b/cibuildwheel/util/cmd.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"cibuildwheel.errors", "shlex", "shutil", "subprocess"} +__lazy_modules__ = {"shlex", "subprocess"} import os import shlex diff --git a/cibuildwheel/util/file.py b/cibuildwheel/util/file.py index 111b7d909..bba2cc78a 100644 --- a/cibuildwheel/util/file.py +++ b/cibuildwheel/util/file.py @@ -1,17 +1,6 @@ from __future__ import annotations -__lazy_modules__ = { - "certifi", - "cibuildwheel.errors", - "hashlib", - "shutil", - "ssl", - "tarfile", - "typing", - "urllib", - "urllib.request", - "zipfile", -} +__lazy_modules__ = {"certifi", "hashlib", "ssl", "tarfile", "urllib", "urllib.request", "zipfile"} import hashlib diff --git a/cibuildwheel/util/helpers.py b/cibuildwheel/util/helpers.py index d6ed17971..498a30543 100644 --- a/cibuildwheel/util/helpers.py +++ b/cibuildwheel/util/helpers.py @@ -1,6 +1,6 @@ from __future__ import annotations -__lazy_modules__ = {"collections", "itertools", "re", "shlex", "textwrap"} +__lazy_modules__ = {"itertools", "shlex"} import dataclasses import itertools diff --git a/cibuildwheel/util/packaging.py b/cibuildwheel/util/packaging.py index 58d2a2818..77a36c351 100644 --- a/cibuildwheel/util/packaging.py +++ b/cibuildwheel/util/packaging.py @@ -1,12 +1,6 @@ from __future__ import annotations -__lazy_modules__ = { - "cibuildwheel.util.cmd", - "cibuildwheel.util.helpers", - "packaging", - "packaging.utils", - "shlex", -} +__lazy_modules__ = {"cibuildwheel.util.cmd", "packaging", "packaging.utils", "shlex"} import shlex from dataclasses import dataclass, field diff --git a/cibuildwheel/util/python_build_standalone.py b/cibuildwheel/util/python_build_standalone.py index 2e8c7fb82..fe5a7c95b 100644 --- a/cibuildwheel/util/python_build_standalone.py +++ b/cibuildwheel/util/python_build_standalone.py @@ -2,7 +2,6 @@ __lazy_modules__ = { "cibuildwheel.util.file", - "cibuildwheel.util.resources", "filelock", "fnmatch", "hashlib", diff --git a/cibuildwheel/util/resources.py b/cibuildwheel/util/resources.py index 3bbef7537..dd3d151f0 100644 --- a/cibuildwheel/util/resources.py +++ b/cibuildwheel/util/resources.py @@ -1,7 +1,5 @@ from __future__ import annotations -__lazy_modules__ = {"tomllib"} - import functools import tomllib from pathlib import Path diff --git a/cibuildwheel/venv.py b/cibuildwheel/venv.py index 2867edd7a..c1e418614 100644 --- a/cibuildwheel/venv.py +++ b/cibuildwheel/venv.py @@ -1,18 +1,13 @@ from __future__ import annotations __lazy_modules__ = { - "cibuildwheel.util", "cibuildwheel.util.cmd", "cibuildwheel.util.file", - "contextlib", "filelock", "packaging", "packaging.markers", "packaging.requirements", "packaging.version", - "pathlib", - "shutil", - "tomllib", } import contextlib diff --git a/noxfile.py b/noxfile.py index 319f1b915..7fef18050 100755 --- a/noxfile.py +++ b/noxfile.py @@ -205,5 +205,54 @@ def build(session: nox.Session) -> None: session.run("python", "-m", "build") +@nox.session(default=False, reuse_venv=True, venv_backend="uv", python="3.15") +def update_lazy_modules(session: nox.Session) -> None: + """ + Update the lazy modules exclusion list in pyproject.toml + """ + session.install("-e.") + env = os.environ.copy() + env["PYTHON_LAZY_IMPORTS"] = "all" + output_default = session.run("python", "-vI", "/dev/null", silent=True) + assert isinstance(output_default, str) + output_help = session.run("python", "-vm", "cibuildwheel", "--help", env=env, silent=True) + assert isinstance(output_help, str) + + def parse_modules(output: str) -> set[str]: + result = set() + destroy = "# destroy " + for line in output.splitlines(): + if line.startswith(destroy): + name = line[len(destroy) :] + result.add(name) + return result + + modules_default = parse_modules(output_default) + modules_help = parse_modules(output_help) + modules = set() + # do not include platform specific modules in the exclusion list + modules_default |= { + "nt", # Windows + "ntpath", # Windows + "posix", # POSIX (Linux/macOS) + "posixpath", # POSIX (Linux/macOS) + } + for name in modules_help - modules_default: + extern_private = name.startswith("_") or ("._" in name and "cibuildwheel" not in name) + if extern_private: + continue + modules.add(f'"{name}"') + exclude_modules = ", ".join(sorted(modules)) + # This is not very robust but should be enough for our simple configuration for now + pyproject_toml_path = Path("pyproject.toml") + pyproject_toml = pyproject_toml_path.read_text().splitlines() + for i in range(len(pyproject_toml)): + if pyproject_toml[i].strip().startswith("lazy-exclude-modules = "): + pyproject_toml[i] = f"lazy-exclude-modules = [{exclude_modules}]" + break + pyproject_toml.append("") + pyproject_toml_path.write_bytes("\n".join(pyproject_toml).encode()) + + if __name__ == "__main__": nox.main() diff --git a/pyproject.toml b/pyproject.toml index 6b499a9cd..9fded469f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -257,3 +257,9 @@ skip = [ 'htmlcov', 'all_known_setup.yaml', ] + +[tool.flake8-lazy.standalone] +apply = "set" +line-length = 100 +# the following line shall only be update using `nox -s update_lazy_modules`. do not reformat +lazy-exclude-modules = ["annotationlib", "argparse", "ast", "bz2", "cibuildwheel", "cibuildwheel.architecture", "cibuildwheel.environment", "cibuildwheel.errors", "cibuildwheel.frontend", "cibuildwheel.oci_container", "cibuildwheel.options", "cibuildwheel.selector", "cibuildwheel.util", "cibuildwheel.util.helpers", "cibuildwheel.util.packaging", "cibuildwheel.util.resources", "collections", "compression", "compression.zstd", "contextlib", "copyreg", "dataclasses", "enum", "fcntl", "functools", "gettext", "grp", "importlib", "importlib.util", "io", "keyword", "locale", "lzma", "operator", "pathlib", "pwd", "re", "reprlib", "runpy", "shutil", "textwrap", "tomllib", "types", "typing", "zlib"]