diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index a770a2569..ef6f03446 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -12,6 +12,7 @@ import cibuildwheel.macos import cibuildwheel.windows from cibuildwheel.environment import EnvironmentParseError, parse_environment +from cibuildwheel.typing import PLATFORMS, PlatStr, assert_never from cibuildwheel.util import ( Architecture, BuildOptions, @@ -19,8 +20,8 @@ DependencyConstraints, Unbuffered, detect_ci_provider, - resources_dir, read_python_configs, + resources_dir, ) @@ -47,6 +48,8 @@ def get_option_from_environment(option_name: str, platform: Optional[str] = None def main() -> None: + platform: PlatStr + parser = argparse.ArgumentParser( description='Build wheels for all the platforms.', epilog=''' @@ -121,7 +124,7 @@ def main() -> None: file=sys.stderr) exit(2) - if platform not in {"linux", "macos", "windows"}: + if platform not in PLATFORMS: print(f'cibuildwheel: Unsupported platform: {platform}', file=sys.stderr) exit(2) @@ -132,8 +135,10 @@ def main() -> None: repair_command_default = 'auditwheel repair -w {dest_dir} {wheel}' elif platform == 'macos': repair_command_default = 'delocate-listdeps {wheel} && delocate-wheel --require-archs x86_64 -w {dest_dir} {wheel}' - else: + elif platform == 'windows': repair_command_default = '' + else: + assert_never(platform) build_config, skip_config = os.environ.get('CIBW_BUILD', '*'), os.environ.get('CIBW_SKIP', '') environment_config = get_option_from_environment('CIBW_ENVIRONMENT', platform=platform, default='') @@ -255,6 +260,8 @@ def main() -> None: cibuildwheel.windows.build(build_options) elif platform == 'macos': cibuildwheel.macos.build(build_options) + else: + assert_never(platform) def detect_obsolete_options() -> None: diff --git a/cibuildwheel/linux.py b/cibuildwheel/linux.py index 1db959d3c..3d7e6a619 100644 --- a/cibuildwheel/linux.py +++ b/cibuildwheel/linux.py @@ -2,7 +2,7 @@ import sys import textwrap from pathlib import Path, PurePath -from typing import List, NamedTuple, Dict, Set +from typing import Dict, List, NamedTuple, Set from .docker_container import DockerContainer from .logger import log diff --git a/cibuildwheel/typing.py b/cibuildwheel/typing.py index ffe8afb68..8c07d282f 100644 --- a/cibuildwheel/typing.py +++ b/cibuildwheel/typing.py @@ -1,6 +1,13 @@ import os import subprocess -from typing import TYPE_CHECKING, Union +import sys +from typing import TYPE_CHECKING, NoReturn, Set, Union + +if sys.version_info < (3, 8): + from typing_extensions import Final, Literal +else: + from typing import Final, Literal + if TYPE_CHECKING: PopenBytes = subprocess.Popen[bytes] @@ -8,3 +15,11 @@ else: PopenBytes = subprocess.Popen PathOrStr = Union[str, "os.PathLike[str]"] + + +PlatStr = Literal["linux", "macos", "windows"] +PLATFORMS: Final[Set[PlatStr]] = {"linux", "macos", "windows"} + + +def assert_never(value: NoReturn) -> NoReturn: + assert False, f'Unhandled value: {value} ({type(value).__name__})' # noqa: B011 diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index 3129d9228..fc4d50a0c 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -3,7 +3,6 @@ import platform as platform_module import re import ssl -import sys import textwrap import urllib.request from enum import Enum @@ -16,12 +15,7 @@ import toml from .environment import ParsedEnvironment -from .typing import PathOrStr - -if sys.version_info < (3, 8): - from typing_extensions import Literal -else: - from typing import Literal +from .typing import PathOrStr, PlatStr def prepare_command(command: str, **kwargs: PathOrStr) -> str: @@ -158,7 +152,7 @@ def __lt__(self, other: "Architecture") -> bool: return self.value < other.value @staticmethod - def parse_config(config: str, platform: str) -> 'Set[Architecture]': + def parse_config(config: str, platform: PlatStr) -> 'Set[Architecture]': result = set() for arch_str in re.split(r'[\s,]+', config): if arch_str == 'auto': @@ -168,7 +162,7 @@ def parse_config(config: str, platform: str) -> 'Set[Architecture]': return result @staticmethod - def auto_archs(platform: str) -> 'Set[Architecture]': + def auto_archs(platform: PlatStr) -> 'Set[Architecture]': native_architecture = Architecture(platform_module.machine()) result = {native_architecture} if platform == 'linux' and native_architecture == Architecture.x86_64: @@ -263,7 +257,7 @@ def detect_ci_provider() -> Optional[CIProvider]: def allowed_architectures_check( - name: Literal['linux', 'macos', 'windows'], + name: PlatStr, options: BuildOptions, ) -> None: diff --git a/setup.cfg b/setup.cfg index 3ba3b866d..2b23a5e90 100644 --- a/setup.cfg +++ b/setup.cfg @@ -112,6 +112,10 @@ ignore_missing_imports = True [mypy-bashlex.*] ignore_missing_imports = True +# Has type stubs, but no pyproject.toml or .pyi files in the wheel. +[mypy-toml.*] +ignore_missing_imports = True + [tool:isort] profile=black multi_line_output=3