Skip to content

Commit

Permalink
chore: more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jan 6, 2021
1 parent 97ddab5 commit b1f1f23
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
13 changes: 10 additions & 3 deletions cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
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,
BuildSelector,
DependencyConstraints,
Unbuffered,
detect_ci_provider,
resources_dir,
read_python_configs,
resources_dir,
)


Expand All @@ -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='''
Expand Down Expand Up @@ -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)

Expand All @@ -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='')
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion cibuildwheel/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion cibuildwheel/typing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
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]
PathOrStr = Union[str, os.PathLike[str]]
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
14 changes: 4 additions & 10 deletions cibuildwheel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import platform as platform_module
import re
import ssl
import sys
import textwrap
import urllib.request
from enum import Enum
Expand All @@ -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:
Expand Down Expand Up @@ -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':
Expand All @@ -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:
Expand Down Expand Up @@ -263,7 +257,7 @@ def detect_ci_provider() -> Optional[CIProvider]:


def allowed_architectures_check(
name: Literal['linux', 'macos', 'windows'],
name: PlatStr,
options: BuildOptions,
) -> None:

Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b1f1f23

Please sign in to comment.