From 5bdb21b7054afbe66daf2d4792d3cc7f174b728b Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Fri, 10 Apr 2020 01:44:02 +0200 Subject: [PATCH] More type annotations after rebasing --- cibuildwheel/__main__.py | 9 +++------ cibuildwheel/linux.py | 8 ++++---- cibuildwheel/macos.py | 5 +++-- cibuildwheel/util.py | 11 +++++++---- cibuildwheel/windows.py | 5 +++-- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index 83e57f03..17fdf393 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -134,7 +134,7 @@ def main() -> None: dependency_versions = get_option_from_environment('CIBW_DEPENDENCY_VERSIONS', platform=platform, default='pinned') if dependency_versions == 'pinned': - dependency_constraints = DependencyConstraints.with_defaults() + dependency_constraints = DependencyConstraints.with_defaults() # type: Optional[DependencyConstraints] elif dependency_versions == 'latest': dependency_constraints = None else: @@ -169,6 +169,7 @@ def main() -> None: print_build_identifiers(platform, build_selector) exit(0) + manylinux_images = None # type: Optional[Dict[str, str]] if platform == 'linux': pinned_docker_images_file = os.path.join( os.path.dirname(__file__), 'resources', 'pinned_docker_images.cfg' @@ -181,8 +182,7 @@ def main() -> None: # 'pypy_x86_64': {'manylinux2010': '...' } # ... } - manylinux_images = {} # type: Optional[Dict[str, str]] - assert manylinux_images is not None # Weird problem with mypy + manylinux_images = {} for build_platform in ['x86_64', 'i686', 'pypy_x86_64', 'aarch64', 'ppc64le', 's390x']: pinned_images = all_pinned_docker_images[build_platform] @@ -200,9 +200,6 @@ def main() -> None: manylinux_images[build_platform] = image - else: - manylinux_images = None - build_options = BuildOptions( package_dir=package_dir, output_dir=output_dir, diff --git a/cibuildwheel/linux.py b/cibuildwheel/linux.py index 33cd493d..38ecf943 100644 --- a/cibuildwheel/linux.py +++ b/cibuildwheel/linux.py @@ -6,7 +6,7 @@ import sys import textwrap import uuid -from typing import Callable, List, NamedTuple +from typing import Callable, List, NamedTuple, Optional, Union from .util import ( BuildOptions, @@ -15,14 +15,14 @@ from .util import ( ) -def call(args, input=None, universal_newlines=False): +def call(args: List[str], input: Optional[Union[str, bytes]] = None, universal_newlines: bool = False) -> None: print('+ ' + ' '.join(shlex.quote(a) for a in args)) subprocess.run( args, input=input, universal_newlines=universal_newlines, check=True ) -def matches_platform(identifier): +def matches_platform(identifier: str) -> bool: pm = platform.machine() if pm == "x86_64": # x86_64 machines can run i686 docker containers @@ -79,7 +79,7 @@ def get_python_configurations(build_selector: Callable[[str], bool]) -> List[Pyt return [c for c in python_configurations if matches_platform(c.identifier) and build_selector(c.identifier)] -def build(options: BuildOptions): +def build(options: BuildOptions) -> None: try: subprocess.check_call(['docker', '--version']) except Exception: diff --git a/cibuildwheel/macos.py b/cibuildwheel/macos.py index 2e0a2535..911d9978 100644 --- a/cibuildwheel/macos.py +++ b/cibuildwheel/macos.py @@ -8,6 +8,7 @@ from glob import glob from typing import Callable, Dict, List, Optional, NamedTuple, Union +from .environment import ParsedEnvironment from .util import ( BuildOptions, download, @@ -106,7 +107,7 @@ def install_pypy(version: str, url: str) -> str: return installation_bin_path -def setup_python(python_configuration, dependency_constraint_flags, environment): +def setup_python(python_configuration: PythonConfiguration, dependency_constraint_flags: List[str], environment: ParsedEnvironment) -> Dict[str, str]: if python_configuration.identifier.startswith('cp'): installation_bin_path = install_cpython(python_configuration.version, python_configuration.url) elif python_configuration.identifier.startswith('pp'): @@ -164,7 +165,7 @@ def setup_python(python_configuration, dependency_constraint_flags, environment) return env -def build(options: BuildOptions): +def build(options: BuildOptions) -> None: temp_dir = tempfile.mkdtemp(prefix='cibuildwheel') built_wheel_dir = os.path.join(temp_dir, 'built_wheel') repaired_wheel_dir = os.path.join(temp_dir, 'repaired_wheel') diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index 21b76f80..33e45adf 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -3,7 +3,7 @@ import urllib.request from fnmatch import fnmatch from time import sleep -from typing import Dict, List, NamedTuple, Optional +from typing import Dict, List, NamedTuple, Optional, Type, TypeVar from .environment import ParsedEnvironment @@ -82,18 +82,21 @@ def download(url: str, dest: str) -> None: response.close() +DependencyConstraints_T = TypeVar('DependencyConstraints_T', bound='DependencyConstraints') + + class DependencyConstraints: - def __init__(self, base_file_path): + def __init__(self, base_file_path: str): assert os.path.exists(base_file_path) self.base_file_path = os.path.abspath(base_file_path) @classmethod - def with_defaults(cls): + def with_defaults(cls: Type[DependencyConstraints_T]) -> DependencyConstraints_T: return cls( base_file_path=os.path.join(os.path.dirname(__file__), 'resources', 'constraints.txt') ) - def get_for_python_version(self, version): + def get_for_python_version(self, version: str) -> str: version_parts = version.split('.') # try to find a version-specific dependency file e.g. if diff --git a/cibuildwheel/windows.py b/cibuildwheel/windows.py index cb78fe60..ffa8152a 100644 --- a/cibuildwheel/windows.py +++ b/cibuildwheel/windows.py @@ -8,6 +8,7 @@ from zipfile import ZipFile from typing import Callable, Dict, List, Optional, NamedTuple +from .environment import ParsedEnvironment from .util import ( BuildOptions, download, @@ -93,7 +94,7 @@ def install_pypy(version: str, arch: str, url: Optional[str]) -> str: return installation_path -def setup_python(python_configuration, dependency_constraint_flags, environment): +def setup_python(python_configuration: PythonConfiguration, dependency_constraint_flags: List[str], environment: ParsedEnvironment) -> Dict[str, str]: nuget = 'C:\\cibw\\nuget.exe' if not os.path.exists(nuget): download('https://dist.nuget.org/win-x86-commandline/latest/nuget.exe', nuget) @@ -145,7 +146,7 @@ def setup_python(python_configuration, dependency_constraint_flags, environment) return env -def build(options: BuildOptions): +def build(options: BuildOptions) -> None: temp_dir = tempfile.mkdtemp(prefix='cibuildwheel') built_wheel_dir = os.path.join(temp_dir, 'built_wheel') repaired_wheel_dir = os.path.join(temp_dir, 'repaired_wheel')