Skip to content

Commit b1f1f23

Browse files
committed
chore: more typing
1 parent 97ddab5 commit b1f1f23

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

cibuildwheel/__main__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
import cibuildwheel.macos
1313
import cibuildwheel.windows
1414
from cibuildwheel.environment import EnvironmentParseError, parse_environment
15+
from cibuildwheel.typing import PLATFORMS, PlatStr, assert_never
1516
from cibuildwheel.util import (
1617
Architecture,
1718
BuildOptions,
1819
BuildSelector,
1920
DependencyConstraints,
2021
Unbuffered,
2122
detect_ci_provider,
22-
resources_dir,
2323
read_python_configs,
24+
resources_dir,
2425
)
2526

2627

@@ -47,6 +48,8 @@ def get_option_from_environment(option_name: str, platform: Optional[str] = None
4748

4849

4950
def main() -> None:
51+
platform: PlatStr
52+
5053
parser = argparse.ArgumentParser(
5154
description='Build wheels for all the platforms.',
5255
epilog='''
@@ -121,7 +124,7 @@ def main() -> None:
121124
file=sys.stderr)
122125
exit(2)
123126

124-
if platform not in {"linux", "macos", "windows"}:
127+
if platform not in PLATFORMS:
125128
print(f'cibuildwheel: Unsupported platform: {platform}', file=sys.stderr)
126129
exit(2)
127130

@@ -132,8 +135,10 @@ def main() -> None:
132135
repair_command_default = 'auditwheel repair -w {dest_dir} {wheel}'
133136
elif platform == 'macos':
134137
repair_command_default = 'delocate-listdeps {wheel} && delocate-wheel --require-archs x86_64 -w {dest_dir} {wheel}'
135-
else:
138+
elif platform == 'windows':
136139
repair_command_default = ''
140+
else:
141+
assert_never(platform)
137142

138143
build_config, skip_config = os.environ.get('CIBW_BUILD', '*'), os.environ.get('CIBW_SKIP', '')
139144
environment_config = get_option_from_environment('CIBW_ENVIRONMENT', platform=platform, default='')
@@ -255,6 +260,8 @@ def main() -> None:
255260
cibuildwheel.windows.build(build_options)
256261
elif platform == 'macos':
257262
cibuildwheel.macos.build(build_options)
263+
else:
264+
assert_never(platform)
258265

259266

260267
def detect_obsolete_options() -> None:

cibuildwheel/linux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import textwrap
44
from pathlib import Path, PurePath
5-
from typing import List, NamedTuple, Dict, Set
5+
from typing import Dict, List, NamedTuple, Set
66

77
from .docker_container import DockerContainer
88
from .logger import log

cibuildwheel/typing.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import os
22
import subprocess
3-
from typing import TYPE_CHECKING, Union
3+
import sys
4+
from typing import TYPE_CHECKING, NoReturn, Set, Union
5+
6+
if sys.version_info < (3, 8):
7+
from typing_extensions import Final, Literal
8+
else:
9+
from typing import Final, Literal
10+
411

512
if TYPE_CHECKING:
613
PopenBytes = subprocess.Popen[bytes]
714
PathOrStr = Union[str, os.PathLike[str]]
815
else:
916
PopenBytes = subprocess.Popen
1017
PathOrStr = Union[str, "os.PathLike[str]"]
18+
19+
20+
PlatStr = Literal["linux", "macos", "windows"]
21+
PLATFORMS: Final[Set[PlatStr]] = {"linux", "macos", "windows"}
22+
23+
24+
def assert_never(value: NoReturn) -> NoReturn:
25+
assert False, f'Unhandled value: {value} ({type(value).__name__})' # noqa: B011

cibuildwheel/util.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import platform as platform_module
44
import re
55
import ssl
6-
import sys
76
import textwrap
87
import urllib.request
98
from enum import Enum
@@ -16,12 +15,7 @@
1615
import toml
1716

1817
from .environment import ParsedEnvironment
19-
from .typing import PathOrStr
20-
21-
if sys.version_info < (3, 8):
22-
from typing_extensions import Literal
23-
else:
24-
from typing import Literal
18+
from .typing import PathOrStr, PlatStr
2519

2620

2721
def prepare_command(command: str, **kwargs: PathOrStr) -> str:
@@ -158,7 +152,7 @@ def __lt__(self, other: "Architecture") -> bool:
158152
return self.value < other.value
159153

160154
@staticmethod
161-
def parse_config(config: str, platform: str) -> 'Set[Architecture]':
155+
def parse_config(config: str, platform: PlatStr) -> 'Set[Architecture]':
162156
result = set()
163157
for arch_str in re.split(r'[\s,]+', config):
164158
if arch_str == 'auto':
@@ -168,7 +162,7 @@ def parse_config(config: str, platform: str) -> 'Set[Architecture]':
168162
return result
169163

170164
@staticmethod
171-
def auto_archs(platform: str) -> 'Set[Architecture]':
165+
def auto_archs(platform: PlatStr) -> 'Set[Architecture]':
172166
native_architecture = Architecture(platform_module.machine())
173167
result = {native_architecture}
174168
if platform == 'linux' and native_architecture == Architecture.x86_64:
@@ -263,7 +257,7 @@ def detect_ci_provider() -> Optional[CIProvider]:
263257

264258

265259
def allowed_architectures_check(
266-
name: Literal['linux', 'macos', 'windows'],
260+
name: PlatStr,
267261
options: BuildOptions,
268262
) -> None:
269263

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ ignore_missing_imports = True
112112
[mypy-bashlex.*]
113113
ignore_missing_imports = True
114114

115+
# Has type stubs, but no pyproject.toml or .pyi files in the wheel.
116+
[mypy-toml.*]
117+
ignore_missing_imports = True
118+
115119
[tool:isort]
116120
profile=black
117121
multi_line_output=3

0 commit comments

Comments
 (0)