From 5cbe3e225410790a41abf57fdc11263a9e9953f9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 24 Mar 2021 18:23:27 -0400 Subject: [PATCH] style: apply some cleanups from Sourcery.ai (#626) * style: apply some fixes from Sorcery * Apply suggestions from code review Co-authored-by: Yannick Jadoul Co-authored-by: Joe Rickerby Co-authored-by: Yannick Jadoul Co-authored-by: Joe Rickerby --- bin/update_pythons.py | 3 +-- cibuildwheel/__main__.py | 5 ++--- cibuildwheel/bashlex_eval.py | 4 +--- cibuildwheel/extra.py | 13 ++++++------ cibuildwheel/linux.py | 2 +- cibuildwheel/macos.py | 24 +++++++++++----------- cibuildwheel/util.py | 4 +--- cibuildwheel/windows.py | 2 +- test/test_dependency_versions.py | 13 ++++++------ unit_test/docker_container_test.py | 2 +- unit_test/main_tests/main_options_test.py | 2 +- unit_test/main_tests/main_platform_test.py | 8 ++------ 12 files changed, 35 insertions(+), 47 deletions(-) diff --git a/bin/update_pythons.py b/bin/update_pythons.py index 9fa0a234..61f95c71 100755 --- a/bin/update_pythons.py +++ b/bin/update_pythons.py @@ -91,12 +91,11 @@ class WindowsVersions: version = versions[-1] identifier = f"cp{version.major}{version.minor}-{self.arch}" - result = ConfigWinCP( + return ConfigWinCP( identifier=identifier, version=str(version), arch=self.arch_str, ) - return result class PyPyVersions: diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index 3ed4c383..7fe64eaf 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -367,9 +367,8 @@ def detect_warnings(platform: str, build_options: BuildOptions) -> List[str]: for option_name in ['test_command', 'before_build']: option_value = getattr(build_options, option_name) - if option_value: - if '{python}' in option_value or '{pip}' in option_value: - warnings.append(option_name + ": '{python}' and '{pip}' are no longer needed, and will be removed in a future release. Simply use 'python' or 'pip' instead.") + if option_value and ('{python}' in option_value or '{pip}' in option_value): + warnings.append(option_name + ": '{python}' and '{pip}' are no longer needed, and will be removed in a future release. Simply use 'python' or 'pip' instead.") return warnings diff --git a/cibuildwheel/bashlex_eval.py b/cibuildwheel/bashlex_eval.py index d82b3269..79386531 100644 --- a/cibuildwheel/bashlex_eval.py +++ b/cibuildwheel/bashlex_eval.py @@ -85,9 +85,7 @@ def evaluate_nodes_as_compound_command(nodes: Sequence[bashlex.ast.node], contex if node.kind == 'command': result += evaluate_command_node(node, context=context) elif node.kind == 'operator': - if node.op == ';': - pass - else: + if node.op != ';': raise ValueError(f'Unsupported bash operator: "{node.op}"') else: raise ValueError(f'Unsupported bash node in compound command: "{node.kind}"') diff --git a/cibuildwheel/extra.py b/cibuildwheel/extra.py index 3c2ef4fb..ea106130 100644 --- a/cibuildwheel/extra.py +++ b/cibuildwheel/extra.py @@ -14,11 +14,10 @@ class InlineArrayDictEncoder(toml.encoder.TomlEncoder): # type: ignore self.dump_funcs[Version] = lambda v: f'"{v}"' def dump_sections(self, o: Dict[str, Any], sup: str) -> Any: - if all(isinstance(a, list) for a in o.values()): - val = "" - for k, v in o.items(): - inner = ",\n ".join(self.dump_inline_table(d_i).strip() for d_i in v) - val += f"{k} = [\n {inner},\n]\n" - return val, self._dict() - else: + if not all(isinstance(a, list) for a in o.values()): return super().dump_sections(o, sup) + val = "" + for k, v in o.items(): + inner = ",\n ".join(self.dump_inline_table(d_i).strip() for d_i in v) + val += f"{k} = [\n {inner},\n]\n" + return val, self._dict() diff --git a/cibuildwheel/linux.py b/cibuildwheel/linux.py index 1e4e6f73..7ba4f5cc 100644 --- a/cibuildwheel/linux.py +++ b/cibuildwheel/linux.py @@ -253,5 +253,5 @@ def troubleshoot(package_dir: Path, error: Exception) -> None: '''), file=sys.stderr) print(' Files detected:') - print('\n'.join([f' {f}' for f in so_files])) + print('\n'.join(f' {f}' for f in so_files)) print('') diff --git a/cibuildwheel/macos.py b/cibuildwheel/macos.py index a51d0b5e..bce16706 100644 --- a/cibuildwheel/macos.py +++ b/cibuildwheel/macos.py @@ -257,23 +257,23 @@ def setup_python(python_configuration: PythonConfiguration, # set ARCHFLAGS otherwise CPython sets it to `-arch x86_64` env.setdefault('ARCHFLAGS', '-arch arm64 -arch x86_64') - if python_configuration.identifier.endswith('arm64') or python_configuration.identifier.endswith('universal2'): - if get_macos_version() < (10, 16) and 'SDKROOT' not in env: - # xcode 12.2 or higher can build arm64 on macos 10.15 or below, but - # needs the correct SDK selected. - sdks = get_macos_sdks() + building_arm64 = python_configuration.identifier.endswith('arm64') or python_configuration.identifier.endswith('universal2') + if building_arm64 and get_macos_version() < (10, 16) and 'SDKROOT' not in env: + # xcode 12.2 or higher can build arm64 on macos 10.15 or below, but + # needs the correct SDK selected. + sdks = get_macos_sdks() - # Different versions of Xcode contain different SDK versions... - # we're happy with anything newer than macOS 11.0 - arm64_compatible_sdks = [s for s in sdks if not s.startswith('macosx10.')] + # Different versions of Xcode contain different SDK versions... + # we're happy with anything newer than macOS 11.0 + arm64_compatible_sdks = [s for s in sdks if not s.startswith('macosx10.')] - if not arm64_compatible_sdks: - log.warning(unwrap(''' + if not arm64_compatible_sdks: + log.warning(unwrap(''' SDK for building arm64-compatible wheels not found. You need Xcode 12.2 or later to build universal2 or arm64 wheels. ''')) - else: - env.setdefault('SDKROOT', arm64_compatible_sdks[0]) + else: + env.setdefault('SDKROOT', arm64_compatible_sdks[0]) log.step('Installing build tools...') call(['pip', 'install', '--upgrade', 'setuptools', 'wheel', 'delocate', *dependency_constraint_flags], env=env) diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index de7eb445..6ec7fe91 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -209,9 +209,7 @@ class NonPlatformWheelError(Exception): def strtobool(val: str) -> bool: - if val.lower() in ('y', 'yes', 't', 'true', 'on', '1'): - return True - return False + return val.lower() in {'y', 'yes', 't', 'true', 'on', '1'} class CIProvider(Enum): diff --git a/cibuildwheel/windows.py b/cibuildwheel/windows.py index fce0838b..8f657d71 100644 --- a/cibuildwheel/windows.py +++ b/cibuildwheel/windows.py @@ -44,7 +44,7 @@ def shell(command: str, env: Optional[Dict[str, str]] = None, cwd: Optional[str] def get_nuget_args(version: str, arch: str) -> List[str]: python_name = 'python' if version[0] == '3' else 'python2' if arch == '32': - python_name = python_name + 'x86' + python_name += 'x86' return [python_name, '-Version', version, '-OutputDirectory', 'C:\\cibw\\python'] diff --git a/test/test_dependency_versions.py b/test/test_dependency_versions.py index 72c10597..44c3ddb0 100644 --- a/test/test_dependency_versions.py +++ b/test/test_dependency_versions.py @@ -40,11 +40,10 @@ VERSION_REGEX = r'([\w-]+)==([^\s]+)' def get_versions_from_constraint_file(constraint_file): constraint_file_text = constraint_file.read_text(encoding='utf8') - versions = {} - for package, version in re.findall(VERSION_REGEX, constraint_file_text): - versions[package] = version - - return versions + return { + package: version + for package, version in re.findall(VERSION_REGEX, constraint_file_text) + } @pytest.mark.parametrize('python_version', ['2.7', '3.5', '3.6', '3.8']) @@ -91,7 +90,7 @@ def test_pinned_versions(tmp_path, python_version): build_environment[env_name] = constraint_versions[package] cibw_environment_option = ' '.join( - [f'{k}={v}' for k, v in build_environment.items()] + f'{k}={v}' for k, v in build_environment.items() ) # build and test the wheels @@ -154,7 +153,7 @@ def test_dependency_constraints_file(tmp_path, python_version): build_environment[env_name] = version cibw_environment_option = ' '.join( - [f'{k}={v}' for k, v in build_environment.items()] + f'{k}={v}' for k, v in build_environment.items() ) # build and test the wheels diff --git a/unit_test/docker_container_test.py b/unit_test/docker_container_test.py index 22771ed2..bd07b50b 100644 --- a/unit_test/docker_container_test.py +++ b/unit_test/docker_container_test.py @@ -94,7 +94,7 @@ def test_binary_output(): data = bytes(output, encoding='utf8', errors='surrogateescape') - for i in range(0, 512): + for i in range(512): assert data[i] == i % 256 # check that environment variables can carry binary data, except null characters diff --git a/unit_test/main_tests/main_options_test.py b/unit_test/main_tests/main_options_test.py index 24559489..dbfc7539 100644 --- a/unit_test/main_tests/main_options_test.py +++ b/unit_test/main_tests/main_options_test.py @@ -134,7 +134,7 @@ def test_repair_command(repair_command, platform_specific, platform, intercepted ]) @pytest.mark.parametrize('platform_specific', [False, True]) def test_environment(environment, platform_specific, platform, intercepted_build_args, monkeypatch): - env_string = ' '.join([f'{k}={v}' for k, v in environment.items()]) + env_string = ' '.join(f'{k}={v}' for k, v in environment.items()) if platform_specific: monkeypatch.setenv('CIBW_ENVIRONMENT_' + platform.upper(), env_string) monkeypatch.setenv('CIBW_ENVIRONMENT', 'overwritten') diff --git a/unit_test/main_tests/main_platform_test.py b/unit_test/main_tests/main_platform_test.py index 1b37663e..45fe7d1e 100644 --- a/unit_test/main_tests/main_platform_test.py +++ b/unit_test/main_tests/main_platform_test.py @@ -123,12 +123,10 @@ def test_archs_platform_native(platform, intercepted_build_args, monkeypatch): main() build_options = intercepted_build_args.args[0] - if platform == 'linux': + if platform in {'linux', 'macos'}: assert build_options.architectures == {Architecture.x86_64} elif platform == 'windows': assert build_options.architectures == {Architecture.AMD64} - elif platform == 'macos': - assert build_options.architectures == {Architecture.x86_64} def test_archs_platform_auto64(platform, intercepted_build_args, monkeypatch): @@ -137,12 +135,10 @@ def test_archs_platform_auto64(platform, intercepted_build_args, monkeypatch): main() build_options = intercepted_build_args.args[0] - if platform == 'linux': + if platform in {'linux', 'macos'}: assert build_options.architectures == {Architecture.x86_64} elif platform == 'windows': assert build_options.architectures == {Architecture.AMD64} - elif platform == 'macos': - assert build_options.architectures == {Architecture.x86_64} def test_archs_platform_auto32(platform, intercepted_build_args, monkeypatch):