diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4188fd65f..a1cb316db 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: exclude: ^cibuildwheel/resources/android/android.patch$ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 6fec9b7edb08fd9989088709d864a7826dc74e80 # frozen: v0.15.12 + rev: 0c7b6c989466a93942def1f84baf36ddfcd60c83 # frozen: v0.15.14 hooks: - id: ruff-check args: ["--fix"] diff --git a/bin/inspect_all_known_projects.py b/bin/inspect_all_known_projects.py index 039647962..c77494302 100755 --- a/bin/inspect_all_known_projects.py +++ b/bin/inspect_all_known_projects.py @@ -24,9 +24,11 @@ the results without the `--online` setting. """ +from __future__ import annotations + import ast -from collections.abc import Iterable, Iterator from pathlib import Path +from typing import TYPE_CHECKING import click import yaml @@ -35,6 +37,9 @@ from cibuildwheel.projectfiles import Analyzer +if TYPE_CHECKING: + from collections.abc import Iterable, Iterator + DIR = Path(__file__).parent.resolve() diff --git a/bin/projects.py b/bin/projects.py index f58d1bdc9..58ac38efb 100755 --- a/bin/projects.py +++ b/bin/projects.py @@ -17,22 +17,26 @@ git diff """ +from __future__ import annotations + import builtins import functools import textwrap import urllib.error import urllib.request import xml.dom.minidom -from collections.abc import Iterable, Mapping, Sequence from datetime import UTC, datetime from io import StringIO from pathlib import Path -from typing import Any, Self, TextIO +from typing import TYPE_CHECKING, Any, Self, TextIO import click import yaml from github import Auth, Github, GithubException +if TYPE_CHECKING: + from collections.abc import Iterable, Mapping, Sequence + ICONS = ( "github", "azurepipelines", diff --git a/bin/update_pythons.py b/bin/update_pythons.py index b99a079ef..d453773ce 100755 --- a/bin/update_pythons.py +++ b/bin/update_pythons.py @@ -12,16 +12,15 @@ # [tool.uv.sources] # cibuildwheel = { path = ".." } # /// - +from __future__ import annotations import difflib import logging import operator import re import tomllib -from collections.abc import Mapping, MutableMapping from pathlib import Path -from typing import Any, Final, Literal, TypedDict +from typing import TYPE_CHECKING, Any, Final, Literal, TypedDict from xml.etree import ElementTree as ET import click @@ -35,6 +34,9 @@ from cibuildwheel.extra import dump_python_configurations, get_pyodide_xbuildenv_info from cibuildwheel.platforms.android import android_triplet +if TYPE_CHECKING: + from collections.abc import Mapping, MutableMapping + log = logging.getLogger("cibw") # Looking up the dir instead of using utils.resources_dir diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index e0def5488..272250cd1 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import argparse import contextlib import dataclasses @@ -8,10 +10,9 @@ import textwrap import traceback import typing -from collections.abc import Generator, Iterable, Sequence from pathlib import Path from tempfile import mkdtemp -from typing import Any, Literal, TextIO +from typing import TYPE_CHECKING, Any, Literal, TextIO import cibuildwheel from cibuildwheel import errors @@ -27,6 +28,9 @@ from cibuildwheel.util.helpers import strtobool from cibuildwheel.util.resources import read_all_configs +if TYPE_CHECKING: + from collections.abc import Generator, Iterable, Sequence + @dataclasses.dataclass class GlobalOptions: diff --git a/cibuildwheel/_compat/tarfile.py b/cibuildwheel/_compat/tarfile.py index 4d5867d16..553073360 100644 --- a/cibuildwheel/_compat/tarfile.py +++ b/cibuildwheel/_compat/tarfile.py @@ -4,8 +4,8 @@ import sys import tarfile +from typing import TYPE_CHECKING -TYPE_CHECKING = False if TYPE_CHECKING: from pathlib import Path diff --git a/cibuildwheel/architecture.py b/cibuildwheel/architecture.py index 655b17623..359f0832d 100644 --- a/cibuildwheel/architecture.py +++ b/cibuildwheel/architecture.py @@ -1,15 +1,20 @@ +from __future__ import annotations + import platform as platform_module import re import shutil import subprocess import sys import typing -from collections.abc import Set from enum import StrEnum, auto -from typing import Final, Literal, Self +from typing import TYPE_CHECKING, Final, Literal, Self from cibuildwheel import errors -from cibuildwheel.typing import PlatformName + +if TYPE_CHECKING: + from collections.abc import Set + + from cibuildwheel.typing import PlatformName PRETTY_NAMES: Final[dict[PlatformName, str]] = { "linux": "Linux", diff --git a/cibuildwheel/audit.py b/cibuildwheel/audit.py index f91b01558..8bae603e8 100644 --- a/cibuildwheel/audit.py +++ b/cibuildwheel/audit.py @@ -1,15 +1,20 @@ +from __future__ import annotations + import subprocess import sys from pathlib import Path +from typing import TYPE_CHECKING from cibuildwheel import errors from cibuildwheel.logger import log -from cibuildwheel.options import BuildOptions from cibuildwheel.util.cmd import call, shell from cibuildwheel.util.helpers import prepare_command from cibuildwheel.util.packaging import is_abi3_wheel from cibuildwheel.venv import activate_virtualenv, find_uv, virtualenv +if TYPE_CHECKING: + from cibuildwheel.options import BuildOptions + def run_audit( *, diff --git a/cibuildwheel/bashlex_eval.py b/cibuildwheel/bashlex_eval.py index 3d1ac1185..d76040524 100644 --- a/cibuildwheel/bashlex_eval.py +++ b/cibuildwheel/bashlex_eval.py @@ -1,16 +1,21 @@ +from __future__ import annotations + import dataclasses import subprocess -from collections.abc import ( - Callable, - Iterable, - Mapping, - Sequence, -) +from typing import TYPE_CHECKING import bashlex -# a function that takes a command and the environment, and returns the result -EnvironmentExecutor = Callable[[list[str], dict[str, str]], str] +if TYPE_CHECKING: + from collections.abc import ( + Callable, + Iterable, + Mapping, + Sequence, + ) + + # a function that takes a command and the environment, and returns the result + EnvironmentExecutor = Callable[[list[str], dict[str, str]], str] def local_environment_executor(command: Sequence[str], env: Mapping[str, str]) -> str: diff --git a/cibuildwheel/environment.py b/cibuildwheel/environment.py index c9e534127..47d2585dc 100644 --- a/cibuildwheel/environment.py +++ b/cibuildwheel/environment.py @@ -1,12 +1,16 @@ +from __future__ import annotations + import dataclasses -from collections.abc import Mapping, Sequence -from typing import Any, Protocol +from typing import TYPE_CHECKING, Any, Protocol import bashlex import bashlex.errors from cibuildwheel import bashlex_eval +if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + class EnvironmentParseError(Exception): pass diff --git a/cibuildwheel/extra.py b/cibuildwheel/extra.py index d075d3217..9e9a57a30 100644 --- a/cibuildwheel/extra.py +++ b/cibuildwheel/extra.py @@ -2,17 +2,22 @@ These are utilities for the `/bin` scripts, not for the `cibuildwheel` program. """ +from __future__ import annotations + import json import time import typing import urllib.error import urllib.request -from collections.abc import Mapping, Sequence from io import StringIO -from typing import Any, NotRequired, Protocol +from typing import TYPE_CHECKING, Any, NotRequired, Protocol from cibuildwheel import __version__ as cibw_version +if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + + __all__ = ("Printable", "dump_python_configurations") diff --git a/cibuildwheel/frontend.py b/cibuildwheel/frontend.py index 11c3e93d4..b7f6654d7 100644 --- a/cibuildwheel/frontend.py +++ b/cibuildwheel/frontend.py @@ -1,12 +1,17 @@ +from __future__ import annotations + import dataclasses import shlex import typing -from collections.abc import Sequence -from typing import Literal, Self, get_args +from typing import TYPE_CHECKING, Literal, Self, get_args -from cibuildwheel.typing import PathOrStr from cibuildwheel.util.helpers import parse_key_value_string, prepare_command +if TYPE_CHECKING: + from collections.abc import Sequence + + from cibuildwheel.typing import PathOrStr + BuildFrontendName = Literal["pip", "build", "build[uv]", "uv"] diff --git a/cibuildwheel/logger.py b/cibuildwheel/logger.py index b6f8b8029..f7381f15f 100644 --- a/cibuildwheel/logger.py +++ b/cibuildwheel/logger.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import codecs import contextlib import dataclasses @@ -9,19 +11,20 @@ import sys import textwrap import time -from collections.abc import Generator from pathlib import Path -from typing import IO, AnyStr, Final, Literal +from typing import IO, TYPE_CHECKING, AnyStr, Final, Literal import humanize from cibuildwheel.ci import CIProvider, detect_ci_provider, filter_ansi_codes -TYPE_CHECKING = False if TYPE_CHECKING: + from collections.abc import Generator + from cibuildwheel.options import Options -FoldPattern = tuple[str, str] + FoldPattern = tuple[str, str] + DEFAULT_FOLD_PATTERN: Final[FoldPattern] = ("{name}", "") FOLD_PATTERNS: Final[dict[str, FoldPattern]] = { "azure": ("##[group]{name}", "##[endgroup]"), @@ -235,7 +238,7 @@ def error(self, error: BaseException | str) -> None: print(f"cibuildwheel: {c.bright_red}error{c.end}: {error}\n", file=sys.stderr) @contextlib.contextmanager - def print_summary(self, *, options: "Options") -> Generator[None, None, None]: + def print_summary(self, *, options: Options) -> Generator[None, None, None]: start = time.time() yield duration = time.time() - start @@ -293,7 +296,7 @@ def _fold_group_identifier(name: str) -> str: # lowercase, shorten return identifier.lower()[:20] - def _github_step_summary(self, duration: float, options: "Options") -> str: + def _github_step_summary(self, duration: float, options: Options) -> str: """ Returns the GitHub step summary, in markdown format. """ diff --git a/cibuildwheel/oci_container.py b/cibuildwheel/oci_container.py index 88144469c..fc07f300a 100644 --- a/cibuildwheel/oci_container.py +++ b/cibuildwheel/oci_container.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import dataclasses import io import json @@ -10,19 +12,23 @@ import textwrap import typing import uuid -from collections.abc import Mapping, Sequence from enum import Enum -from pathlib import Path, PurePath, PurePosixPath -from types import TracebackType -from typing import IO, Literal, Self, assert_never +from pathlib import PurePosixPath +from typing import IO, TYPE_CHECKING, Literal, Self, assert_never from cibuildwheel.ci import CIProvider, detect_ci_provider from cibuildwheel.errors import OCIEngineTooOldError from cibuildwheel.logger import log -from cibuildwheel.typing import PathOrStr from cibuildwheel.util.cmd import call from cibuildwheel.util.helpers import FlexibleVersion, parse_key_value_string, strtobool +if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + from pathlib import Path, PurePath + from types import TracebackType + + from cibuildwheel.typing import PathOrStr + ContainerEngineName = Literal["docker", "podman"] diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index 800c882fa..d4c55e5b5 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import collections import configparser import contextlib @@ -8,9 +10,9 @@ import shlex import textwrap import tomllib -from collections.abc import Callable, Generator, Iterable, Mapping, Sequence, Set +from collections.abc import Mapping, Sequence from pathlib import Path -from typing import Any, Final, Literal, Self, assert_never +from typing import TYPE_CHECKING, Any, Final, Literal, Self, assert_never from packaging.specifiers import SpecifierSet @@ -27,6 +29,9 @@ from cibuildwheel.util.helpers import format_safe, parse_key_value_string, strtobool, unwrap from cibuildwheel.util.packaging import DependencyConstraints +if TYPE_CHECKING: + from collections.abc import Callable, Generator, Iterable, Set + MANYLINUX_ARCHS: Final[tuple[str, ...]] = ( "x86_64", "i686", diff --git a/cibuildwheel/platforms/__init__.py b/cibuildwheel/platforms/__init__.py index 8549a4039..cb31b0c8b 100644 --- a/cibuildwheel/platforms/__init__.py +++ b/cibuildwheel/platforms/__init__.py @@ -1,14 +1,19 @@ +from __future__ import annotations + import sys -from collections.abc import Sequence -from pathlib import Path -from typing import Final, Protocol +from typing import TYPE_CHECKING, Final, Protocol from cibuildwheel import errors -from cibuildwheel.architecture import Architecture -from cibuildwheel.options import Options from cibuildwheel.platforms import android, ios, linux, macos, pyodide, windows -from cibuildwheel.selector import BuildSelector -from cibuildwheel.typing import GenericPythonConfiguration, PlatformName + +if TYPE_CHECKING: + from collections.abc import Sequence + from pathlib import Path + + from cibuildwheel.architecture import Architecture + from cibuildwheel.options import Options + from cibuildwheel.selector import BuildSelector + from cibuildwheel.typing import GenericPythonConfiguration, PlatformName class PlatformModule(Protocol): diff --git a/cibuildwheel/platforms/android.py b/cibuildwheel/platforms/android.py index ae2b2a79f..df8876d58 100644 --- a/cibuildwheel/platforms/android.py +++ b/cibuildwheel/platforms/android.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import csv import hashlib import os @@ -7,14 +9,13 @@ import shutil import subprocess import sysconfig -from collections.abc import Iterable, Iterator, MutableMapping from dataclasses import dataclass from os.path import relpath from pathlib import Path from pprint import pprint from runpy import run_path from textwrap import dedent -from typing import Any +from typing import TYPE_CHECKING, Any from build import ProjectBuilder from build.env import IsolatedEnv @@ -31,8 +32,6 @@ prepare_config_settings, ) from cibuildwheel.logger import log -from cibuildwheel.options import BuildOptions, Options -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.cmd import call, shell from cibuildwheel.util.file import CIBW_CACHE_PATH, copy_test_sources, download, move_file @@ -41,8 +40,13 @@ from cibuildwheel.util.python_build_standalone import create_python_build_standalone_environment from cibuildwheel.venv import constraint_flags, find_uv, virtualenv -RESOURCES_ANDROID = resources.PATH / "android" +if TYPE_CHECKING: + from collections.abc import Iterable, Iterator, MutableMapping + from cibuildwheel.options import BuildOptions, Options + from cibuildwheel.selector import BuildSelector + +RESOURCES_ANDROID = resources.PATH / "android" ANDROID_TRIPLET = { "arm64_v8a": "aarch64-linux-android", "x86_64": "x86_64-linux-android", diff --git a/cibuildwheel/platforms/ios.py b/cibuildwheel/platforms/ios.py index 40c66b877..7cb31d9b1 100644 --- a/cibuildwheel/platforms/ios.py +++ b/cibuildwheel/platforms/ios.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import dataclasses import os import platform @@ -6,25 +8,20 @@ import subprocess import sys import textwrap -from collections.abc import Sequence, Set from pathlib import Path -from typing import assert_never +from typing import TYPE_CHECKING, assert_never from filelock import FileLock from cibuildwheel import errors -from cibuildwheel.architecture import Architecture from cibuildwheel.audit import run_audit -from cibuildwheel.environment import ParsedEnvironment from cibuildwheel.frontend import ( BuildFrontendName, get_build_frontend_extra_flags, prepare_config_settings, ) from cibuildwheel.logger import log -from cibuildwheel.options import Options from cibuildwheel.platforms.macos import install_cpython as install_build_cpython -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.cmd import call, shell, split_command from cibuildwheel.util.file import CIBW_CACHE_PATH, copy_test_sources, download, move_file @@ -32,6 +29,14 @@ from cibuildwheel.util.packaging import find_compatible_wheel from cibuildwheel.venv import constraint_flags, virtualenv +if TYPE_CHECKING: + from collections.abc import Sequence, Set + + from cibuildwheel.architecture import Architecture + from cibuildwheel.environment import ParsedEnvironment + from cibuildwheel.options import Options + from cibuildwheel.selector import BuildSelector + @dataclasses.dataclass(frozen=True, kw_only=True) class PythonConfiguration: diff --git a/cibuildwheel/platforms/linux.py b/cibuildwheel/platforms/linux.py index a9f3e1fad..c00e3c0b4 100644 --- a/cibuildwheel/platforms/linux.py +++ b/cibuildwheel/platforms/linux.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import contextlib import dataclasses import shutil @@ -5,9 +7,8 @@ import sys import textwrap from collections import OrderedDict -from collections.abc import Iterable, Iterator, Sequence, Set from pathlib import Path, PurePath, PurePosixPath -from typing import assert_never +from typing import TYPE_CHECKING, assert_never from cibuildwheel import errors from cibuildwheel.architecture import Architecture @@ -15,15 +16,16 @@ from cibuildwheel.frontend import get_build_frontend_extra_flags, prepare_config_settings from cibuildwheel.logger import log from cibuildwheel.oci_container import OCIContainer, OCIContainerEngineConfig, OCIPlatform -from cibuildwheel.options import BuildOptions, Options -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.file import copy_test_sources from cibuildwheel.util.helpers import prepare_command, unwrap from cibuildwheel.util.packaging import find_compatible_wheel -TYPE_CHECKING = False if TYPE_CHECKING: + from collections.abc import Iterable, Iterator, Sequence, Set + + from cibuildwheel.options import BuildOptions, Options + from cibuildwheel.selector import BuildSelector from cibuildwheel.typing import PathOrStr ARCHITECTURE_OCI_PLATFORM_MAP = { diff --git a/cibuildwheel/platforms/macos.py b/cibuildwheel/platforms/macos.py index bb830a346..ae3bd178a 100644 --- a/cibuildwheel/platforms/macos.py +++ b/cibuildwheel/platforms/macos.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import dataclasses import functools import inspect @@ -8,26 +10,21 @@ import subprocess import sys import typing -from collections.abc import Set from pathlib import Path -from typing import Literal, assert_never +from typing import TYPE_CHECKING, Literal, assert_never from filelock import FileLock from packaging.version import Version from cibuildwheel import errors -from cibuildwheel.architecture import Architecture from cibuildwheel.audit import run_audit from cibuildwheel.ci import detect_ci_provider -from cibuildwheel.environment import ParsedEnvironment from cibuildwheel.frontend import ( BuildFrontendName, get_build_frontend_extra_flags, prepare_config_settings, ) from cibuildwheel.logger import log -from cibuildwheel.options import Options -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.cmd import call, shell from cibuildwheel.util.file import CIBW_CACHE_PATH, copy_test_sources, download, move_file @@ -35,6 +32,14 @@ from cibuildwheel.util.packaging import find_compatible_wheel, get_pip_version from cibuildwheel.venv import constraint_flags, find_uv, target_marker_env, virtualenv +if TYPE_CHECKING: + from collections.abc import Set + + from cibuildwheel.architecture import Architecture + from cibuildwheel.environment import ParsedEnvironment + from cibuildwheel.options import Options + from cibuildwheel.selector import BuildSelector + @functools.cache def get_macos_version() -> tuple[int, int]: diff --git a/cibuildwheel/platforms/pyodide.py b/cibuildwheel/platforms/pyodide.py index 262eac2b8..9e03e5fb1 100644 --- a/cibuildwheel/platforms/pyodide.py +++ b/cibuildwheel/platforms/pyodide.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import dataclasses import functools import json @@ -7,21 +9,17 @@ import sys import tomllib import typing -from collections.abc import Set from pathlib import Path from tempfile import TemporaryDirectory -from typing import Final, TypedDict +from typing import TYPE_CHECKING, Final, TypedDict from filelock import FileLock from cibuildwheel import errors from cibuildwheel.architecture import Architecture from cibuildwheel.audit import run_audit -from cibuildwheel.environment import ParsedEnvironment from cibuildwheel.frontend import get_build_frontend_extra_flags, prepare_config_settings from cibuildwheel.logger import log -from cibuildwheel.options import Options -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.cmd import call, shell from cibuildwheel.util.file import ( @@ -40,6 +38,13 @@ ) from cibuildwheel.venv import constraint_flags, virtualenv +if TYPE_CHECKING: + from collections.abc import Set + + from cibuildwheel.environment import ParsedEnvironment + from cibuildwheel.options import Options + from cibuildwheel.selector import BuildSelector + IS_WIN: Final[bool] = sys.platform.startswith("win") diff --git a/cibuildwheel/platforms/windows.py b/cibuildwheel/platforms/windows.py index 1a858c8fb..7dc48e412 100644 --- a/cibuildwheel/platforms/windows.py +++ b/cibuildwheel/platforms/windows.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import dataclasses import json import os @@ -5,25 +7,21 @@ import shutil import subprocess import textwrap -from collections.abc import MutableMapping, Sequence, Set from functools import cache from pathlib import Path -from typing import assert_never +from typing import TYPE_CHECKING, assert_never from filelock import FileLock from cibuildwheel import errors from cibuildwheel.architecture import Architecture from cibuildwheel.audit import run_audit -from cibuildwheel.environment import ParsedEnvironment from cibuildwheel.frontend import ( BuildFrontendName, get_build_frontend_extra_flags, prepare_config_settings, ) from cibuildwheel.logger import log -from cibuildwheel.options import Options -from cibuildwheel.selector import BuildSelector from cibuildwheel.util import resources from cibuildwheel.util.cmd import call, shell from cibuildwheel.util.file import ( @@ -37,6 +35,13 @@ from cibuildwheel.util.packaging import find_compatible_wheel, get_pip_version from cibuildwheel.venv import constraint_flags, find_uv, target_marker_env, virtualenv +if TYPE_CHECKING: + from collections.abc import MutableMapping, Sequence, Set + + from cibuildwheel.environment import ParsedEnvironment + from cibuildwheel.options import Options + from cibuildwheel.selector import BuildSelector + def get_nuget_args( version: str, arch: str, free_threaded: bool, output_directory: Path diff --git a/cibuildwheel/projectfiles.py b/cibuildwheel/projectfiles.py index 3117486fe..43b7a1a94 100644 --- a/cibuildwheel/projectfiles.py +++ b/cibuildwheel/projectfiles.py @@ -1,11 +1,15 @@ +from __future__ import annotations + import ast import configparser import contextlib -from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING, Any import dependency_groups +if TYPE_CHECKING: + from pathlib import Path + def get_parent(node: ast.AST | None, depth: int = 1) -> ast.AST | None: for _ in range(depth): diff --git a/cibuildwheel/schema.py b/cibuildwheel/schema.py index ec070278b..6bbf0d011 100644 --- a/cibuildwheel/schema.py +++ b/cibuildwheel/schema.py @@ -1,8 +1,13 @@ +from __future__ import annotations + import json -from typing import Any +from typing import TYPE_CHECKING from cibuildwheel.util import resources +if TYPE_CHECKING: + from typing import Any + def get_schema(tool_name: str = "cibuildwheel") -> dict[str, Any]: "Get the stored complete schema for cibuildwheel settings." diff --git a/cibuildwheel/selector.py b/cibuildwheel/selector.py index 5f3853a08..865ada814 100644 --- a/cibuildwheel/selector.py +++ b/cibuildwheel/selector.py @@ -1,13 +1,17 @@ +from __future__ import annotations + import dataclasses import itertools from enum import StrEnum from fnmatch import fnmatch -from typing import Self +from typing import TYPE_CHECKING, Self import bracex -from packaging.specifiers import SpecifierSet from packaging.version import Version +if TYPE_CHECKING: + from packaging.specifiers import SpecifierSet + def selector_matches(patterns: str, string: str) -> bool: """ diff --git a/cibuildwheel/util/cmd.py b/cibuildwheel/util/cmd.py index aba6cf179..8dd05d620 100644 --- a/cibuildwheel/util/cmd.py +++ b/cibuildwheel/util/cmd.py @@ -1,14 +1,19 @@ +from __future__ import annotations + import os import shlex import shutil import subprocess import sys import typing -from collections.abc import Iterator, Mapping -from typing import Final, Literal +from typing import TYPE_CHECKING, Final, Literal from cibuildwheel.errors import FatalError -from cibuildwheel.typing import PathOrStr + +if TYPE_CHECKING: + from collections.abc import Iterator, Mapping + + from cibuildwheel.typing import PathOrStr _IS_WIN: Final[bool] = sys.platform.startswith("win") diff --git a/cibuildwheel/util/file.py b/cibuildwheel/util/file.py index 04e40f3d6..dfd3992c7 100644 --- a/cibuildwheel/util/file.py +++ b/cibuildwheel/util/file.py @@ -1,12 +1,13 @@ +from __future__ import annotations + import os import shutil import ssl import tarfile import time import urllib.request -from collections.abc import Callable from pathlib import Path, PurePath -from typing import Final +from typing import TYPE_CHECKING, Final from zipfile import ZipFile import certifi @@ -14,6 +15,9 @@ from cibuildwheel.errors import FatalError +if TYPE_CHECKING: + from collections.abc import Callable + DEFAULT_CIBW_CACHE_PATH: Final[Path] = user_cache_path(appname="cibuildwheel", appauthor="pypa") CIBW_CACHE_PATH: Final[Path] = Path( os.environ.get("CIBW_CACHE_PATH", DEFAULT_CIBW_CACHE_PATH) diff --git a/cibuildwheel/util/helpers.py b/cibuildwheel/util/helpers.py index 6e17cb2eb..40692864b 100644 --- a/cibuildwheel/util/helpers.py +++ b/cibuildwheel/util/helpers.py @@ -1,13 +1,18 @@ +from __future__ import annotations + import dataclasses import itertools -import os import re import shlex import textwrap from collections import defaultdict -from collections.abc import Sequence +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import os + from collections.abc import Sequence -from cibuildwheel.typing import PathOrStr + from cibuildwheel.typing import PathOrStr def format_safe(template: str, **kwargs: str | os.PathLike[str]) -> str: diff --git a/cibuildwheel/util/packaging.py b/cibuildwheel/util/packaging.py index 1dc71eaf0..0c64b573d 100644 --- a/cibuildwheel/util/packaging.py +++ b/cibuildwheel/util/packaging.py @@ -1,8 +1,9 @@ +from __future__ import annotations + import shlex -from collections.abc import Mapping, Sequence from dataclasses import dataclass, field from pathlib import Path, PurePath -from typing import Literal, Self, TypeVar +from typing import TYPE_CHECKING, Literal, Self, TypeVar from packaging.utils import parse_wheel_filename @@ -10,6 +11,9 @@ from cibuildwheel.util.cmd import call from cibuildwheel.util.helpers import parse_key_value_string, unwrap +if TYPE_CHECKING: + from collections.abc import Mapping, Sequence + @dataclass(kw_only=True) class DependencyConstraints: diff --git a/cibuildwheel/util/python_build_standalone.py b/cibuildwheel/util/python_build_standalone.py index f8dca73ce..1a9ac310b 100644 --- a/cibuildwheel/util/python_build_standalone.py +++ b/cibuildwheel/util/python_build_standalone.py @@ -1,15 +1,20 @@ +from __future__ import annotations + import fnmatch import functools import json import platform import typing -from pathlib import Path +from typing import TYPE_CHECKING from filelock import FileLock from cibuildwheel.util.file import download, extract_tar from cibuildwheel.util.resources import PYTHON_BUILD_STANDALONE_RELEASES +if TYPE_CHECKING: + from pathlib import Path + class PythonBuildStandaloneAsset(typing.TypedDict): name: str diff --git a/cibuildwheel/util/resources.py b/cibuildwheel/util/resources.py index 37775b188..54222aaf2 100644 --- a/cibuildwheel/util/resources.py +++ b/cibuildwheel/util/resources.py @@ -1,9 +1,12 @@ +from __future__ import annotations + import functools import tomllib from pathlib import Path -from typing import Final +from typing import TYPE_CHECKING, Final -from cibuildwheel.typing import PlatformName +if TYPE_CHECKING: + from cibuildwheel.typing import PlatformName PATH: Final[Path] = Path(__file__).parent.parent / "resources" INSTALL_CERTIFI_SCRIPT: Final[Path] = PATH / "install_certifi.py" diff --git a/cibuildwheel/venv.py b/cibuildwheel/venv.py index f38ab11ec..97b093da5 100644 --- a/cibuildwheel/venv.py +++ b/cibuildwheel/venv.py @@ -1,12 +1,13 @@ +from __future__ import annotations + import contextlib import functools import os import shutil import sys import tomllib -from collections.abc import Sequence from pathlib import Path -from typing import Final, cast +from typing import TYPE_CHECKING, Final, cast from filelock import FileLock from packaging.markers import default_environment @@ -17,6 +18,9 @@ from cibuildwheel.util.cmd import call from cibuildwheel.util.file import CIBW_CACHE_PATH, download +if TYPE_CHECKING: + from collections.abc import Sequence + _IS_WIN: Final[bool] = sys.platform.startswith("win") diff --git a/pyproject.toml b/pyproject.toml index fd84620b5..80f8cefc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -224,6 +224,7 @@ flake8-unused-arguments.ignore-variadic-names = true flake8-tidy-imports.ban-relative-imports = "all" flake8-annotations.allow-star-arg-any = true flake8-annotations.mypy-init-return = true +future-annotations = true [tool.ruff.lint.flake8-tidy-imports.banned-api] "typing.Mapping".msg = "Use collections.abc.Mapping instead." @@ -231,11 +232,10 @@ flake8-annotations.mypy-init-return = true "typing.Iterator".msg = "Use collections.abc.Iterator instead." "typing.Sequence".msg = "Use collections.abc.Sequence instead." "typing.Set".msg = "Use collections.abc.Set instead." -"typing.TYPE_CHECKING".msg = "Use TYPE_CHECKING=False instead" [tool.ruff.lint.per-file-ignores] -"unit_test/*" = ["PLC1901", "TID252"] -"test/*" = ["TID252"] +"unit_test/*" = ["PLC1901", "TC", "TID252"] +"test/*" = ["TC", "TID252"] "bin/*" = ["TID251"] "cibuildwheel/resources/install_certifi.py" = ["PTH"] diff --git a/unit_test/main_tests/main_options_test.py b/unit_test/main_tests/main_options_test.py index 6b3701aa6..8e524f77a 100644 --- a/unit_test/main_tests/main_options_test.py +++ b/unit_test/main_tests/main_options_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import sys import tomllib from collections.abc import Mapping @@ -37,7 +39,7 @@ def test_old_free_threaded( @pytest.mark.usefixtures("platform") def test_output_dir( - intercepted_build_args: "ArgsInterceptor", monkeypatch: pytest.MonkeyPatch + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch ) -> None: OUTPUT_DIR = Path("some_output_dir") @@ -49,7 +51,7 @@ def test_output_dir( @pytest.mark.usefixtures("platform") -def test_output_dir_default(intercepted_build_args: "ArgsInterceptor") -> None: +def test_output_dir_default(intercepted_build_args: ArgsInterceptor) -> None: main() assert intercepted_build_args.args[0].globals.output_dir == Path("wheelhouse").resolve() @@ -59,7 +61,7 @@ def test_output_dir_default(intercepted_build_args: "ArgsInterceptor") -> None: @pytest.mark.parametrize("also_set_environment", [False, True]) def test_output_dir_argument( also_set_environment: bool, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: OUTPUT_DIR = Path("some_output_dir") @@ -75,7 +77,7 @@ def test_output_dir_argument( @pytest.mark.usefixtures("platform", "allow_empty") def test_build_selector( - intercepted_build_args: "ArgsInterceptor", monkeypatch: pytest.MonkeyPatch + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setenv("CIBW_BUILD", "cp313-*") monkeypatch.setenv("CIBW_SKIP", "cp39-*") @@ -191,7 +193,7 @@ def test_manylinux_images( image: str | None, full_image: str, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if image is not None: @@ -230,7 +232,7 @@ def test_repair_command( repair_command: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if repair_command is not None: @@ -259,7 +261,7 @@ def test_environment( environment: Mapping[str, str], platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: env_string = " ".join(f"{k}={v}" for k, v in environment.items()) @@ -284,7 +286,7 @@ def test_test_requires( test_requires: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if test_requires is not None: @@ -307,7 +309,7 @@ def test_audit_requires( audit_requires: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if audit_requires is not None: @@ -331,7 +333,7 @@ def test_test_extras( test_extras: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if test_extras is not None: @@ -354,7 +356,7 @@ def test_test_command( test_command: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if test_command is not None: @@ -377,7 +379,7 @@ def test_before_build( before_build: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if before_build is not None: @@ -399,7 +401,7 @@ def test_build_verbosity( build_verbosity: int | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if build_verbosity is not None: @@ -420,7 +422,7 @@ def test_build_verbosity( def test_config_settings( platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: config_settings = ( @@ -507,7 +509,7 @@ def test_before_all( before_all: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, ) -> None: if before_all is not None: @@ -533,7 +535,7 @@ def test_dependency_versions( dependency_versions: str | None, platform_specific: bool, platform: str, - intercepted_build_args: "ArgsInterceptor", + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, ) -> None: @@ -593,7 +595,7 @@ def test_debug_traceback( @pytest.mark.parametrize("method", ["unset", "command_line", "env_var"]) def test_enable( - method: str, intercepted_build_args: "ArgsInterceptor", monkeypatch: pytest.MonkeyPatch + method: str, intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.delenv("CIBW_ENABLE", raising=False) @@ -613,7 +615,7 @@ def test_enable( def test_enable_all( - intercepted_build_args: "ArgsInterceptor", monkeypatch: pytest.MonkeyPatch + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(sys, "argv", [*sys.argv, "--enable", "all"]) monkeypatch.delenv("CIBW_ENABLE", raising=False) @@ -625,7 +627,7 @@ def test_enable_all( def test_enable_arg_inherits( - intercepted_build_args: "ArgsInterceptor", monkeypatch: pytest.MonkeyPatch + intercepted_build_args: ArgsInterceptor, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setenv("CIBW_ENABLE", "pypy graalpy") monkeypatch.setattr(sys, "argv", [*sys.argv, "--enable", "cpython-prerelease"]) @@ -652,7 +654,7 @@ def test_enable_arg_error_message( assert "Valid group names are:" in err -def test_defaults(platform: str, intercepted_build_args: "ArgsInterceptor") -> None: +def test_defaults(platform: str, intercepted_build_args: ArgsInterceptor) -> None: main() build_options: BuildOptions = intercepted_build_args.args[0].build_options(identifier=None)