Skip to content

Commit

Permalink
Merge pull request #1476 from henryiii/henryiii/chore/ruff_ex
Browse files Browse the repository at this point in the history
chore: ruff and typing updates
  • Loading branch information
joerick authored Apr 18, 2023
2 parents 440e18c + 8c5f89c commit dfbc6c3
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 92 deletions.
4 changes: 2 additions & 2 deletions bin/inspect_all_known_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from __future__ import annotations

import ast
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import Iterator

import click
import yaml
Expand Down Expand Up @@ -97,7 +97,7 @@ def save(self, filename: Path | str) -> None:
with open(filename, "w") as f:
yaml.safe_dump(self.contents, f, default_flow_style=False)

def on_each(self, repos: list[str]) -> Iterator[tuple[str, str, str | None]]:
def on_each(self, repos: Iterable[str]) -> Iterator[tuple[str, str, str | None]]:
for repo in repos:
print(f"[bold]{repo}:")
for filename in sorted(self.contents, reverse=True):
Expand Down
9 changes: 5 additions & 4 deletions bin/projects.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import textwrap
import urllib.request
import xml.dom.minidom
from collections.abc import Iterable, Mapping, Sequence
from datetime import datetime
from io import StringIO
from pathlib import Path
Expand All @@ -42,7 +43,7 @@
class Project:
NAME: int = 0

def __init__(self, config: dict[str, Any], github: Github | None = None):
def __init__(self, config: Mapping[str, Any], github: Github | None = None):
try:
self.name: str = config["name"]
self.gh: str = config["gh"]
Expand Down Expand Up @@ -149,7 +150,7 @@ def path_for_icon(icon_name: str, relative_to: Path | None = None) -> Path:


def get_projects(
config: list[dict[str, Any]],
config: Iterable[Mapping[str, Any]],
*,
online: bool = True,
auth: str | None = None,
Expand All @@ -163,7 +164,7 @@ def get_projects(
return sorted((Project(item, github) for item in config), reverse=online)


def render_projects(projects: list[Project], *, dest_path: Path, include_info: bool = True):
def render_projects(projects: Sequence[Project], *, dest_path: Path, include_info: bool = True):
io = StringIO()
print = functools.partial(builtins.print, file=io)

Expand Down Expand Up @@ -191,7 +192,7 @@ def render_projects(projects: list[Project], *, dest_path: Path, include_info: b
def insert_projects_table(
file: Path,
*,
projects: list[Project],
projects: Sequence[Project],
input_filename: str,
include_info: bool = True,
):
Expand Down
Empty file modified bin/update_how_it_works_image.py
100644 → 100755
Empty file.
11 changes: 6 additions & 5 deletions bin/update_pythons.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import difflib
import logging
import sys
from collections.abc import Mapping, MutableMapping
from pathlib import Path
from typing import Any, Union

Expand Down Expand Up @@ -93,7 +94,7 @@ def update_version_windows(self, spec: Specifier) -> ConfigWinCP | None:
unsorted_versions = spec.filter(self.version_dict)
versions = sorted(unsorted_versions, reverse=True)

log.debug(f"Windows {self.arch} {spec} has {', '.join(str(v) for v in versions)}")
log.debug("Windows %s %s has %s", self.arch, spec, ", ".join(str(v) for v in versions))

if not versions:
return None
Expand Down Expand Up @@ -124,7 +125,7 @@ def __init__(self, arch_str: ArchStr):
]
self.arch = arch_str

def get_arch_file(self, release: dict[str, Any]) -> str:
def get_arch_file(self, release: Mapping[str, Any]) -> str:
urls: list[str] = [
rf["download_url"]
for rf in release["files"]
Expand Down Expand Up @@ -250,11 +251,11 @@ def __init__(self) -> None:
self.macos_pypy = PyPyVersions("64")
self.macos_pypy_arm64 = PyPyVersions("ARM64")

def update_config(self, config: dict[str, str]) -> None:
def update_config(self, config: MutableMapping[str, str]) -> None:
identifier = config["identifier"]
version = Version(config["version"])
spec = Specifier(f"=={version.major}.{version.minor}.*")
log.info(f"Reading in '{identifier}' -> {spec} @ {version}")
log.info("Reading in %r -> %s @ %s", str(identifier), spec, version)
orig_config = copy.copy(config)
config_update: AnyConfig | None = None

Expand Down Expand Up @@ -282,7 +283,7 @@ def update_config(self, config: dict[str, str]) -> None:
config.update(**config_update)

if config != orig_config:
log.info(f" Updated {orig_config} to {config}")
log.info(" Updated %s to %s", orig_config, config)


@click.command()
Expand Down
6 changes: 3 additions & 3 deletions bin/update_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def git_ls_remote_versions(url) -> list[VersionTuple]:
try:
version = Version(version_string)
if version.is_devrelease:
log.info(f"Ignoring development release '{version}'")
log.info("Ignoring development release %r", str(version))
continue
if version.is_prerelease:
log.info(f"Ignoring pre-release '{version}'")
log.info("Ignoring pre-release %r", str(version))
continue
versions.append(VersionTuple(version, version_string))
except InvalidVersion:
log.warning(f"Ignoring ref '{ref}'")
log.warning("Ignoring ref %r", ref)
versions.sort(reverse=True)
return versions

Expand Down
6 changes: 3 additions & 3 deletions cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tarfile
import textwrap
import typing
from collections.abc import Sequence, Set
from collections.abc import Iterable, Sequence, Set
from pathlib import Path
from tempfile import mkdtemp

Expand Down Expand Up @@ -337,7 +337,7 @@ def build_in_directory(args: CommandLineArguments) -> None:
log.warning(f"Can't delete temporary folder '{tmp_path}'")


def print_preamble(platform: str, options: Options, identifiers: list[str]) -> None:
def print_preamble(platform: str, options: Options, identifiers: Sequence[str]) -> None:
print(
textwrap.dedent(
"""
Expand Down Expand Up @@ -377,7 +377,7 @@ def get_build_identifiers(
return [config.identifier for config in python_configurations]


def detect_warnings(*, options: Options, identifiers: list[str]) -> list[str]:
def detect_warnings(*, options: Options, identifiers: Iterable[str]) -> list[str]:
warnings = []

# warn about deprecated {python} and {pip}
Expand Down
3 changes: 2 additions & 1 deletion cibuildwheel/architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import platform as platform_module
import re
import sys
from collections.abc import Set
from enum import Enum

from .typing import Final, Literal, PlatformName, assert_never
Expand Down Expand Up @@ -132,7 +133,7 @@ def bitness_archs(platform: PlatformName, bitness: Literal["64", "32"]) -> set[A

def allowed_architectures_check(
platform: PlatformName,
architectures: set[Architecture],
architectures: Set[Architecture],
) -> None:
allowed_architectures = Architecture.all_archs(platform)

Expand Down
13 changes: 8 additions & 5 deletions cibuildwheel/bashlex_eval.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from __future__ import annotations

import subprocess
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass
from typing import Callable, Dict, List, Sequence
from typing import Callable, Dict, List # noqa: TID251

import bashlex

# a function that takes a command and the environment, and returns the result
EnvironmentExecutor = Callable[[List[str], Dict[str, str]], str]


def local_environment_executor(command: list[str], env: dict[str, str]) -> str:
def local_environment_executor(command: Sequence[str], env: Mapping[str, str]) -> str:
return subprocess.run(command, env=env, text=True, stdout=subprocess.PIPE, check=True).stdout


Expand All @@ -22,7 +23,7 @@ class NodeExecutionContext:


def evaluate(
value: str, environment: dict[str, str], executor: EnvironmentExecutor | None = None
value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
) -> str:
if not value:
# empty string evaluates to empty string
Expand All @@ -40,7 +41,9 @@ def evaluate(
return evaluate_node(
value_word_node,
context=NodeExecutionContext(
environment=environment, input=value, executor=executor or local_environment_executor
environment=dict(environment),
input=value,
executor=executor or local_environment_executor,
),
)

Expand Down Expand Up @@ -105,7 +108,7 @@ def evaluate_nodes_as_compound_command(


def evaluate_nodes_as_simple_command(
nodes: list[bashlex.ast.node], context: NodeExecutionContext
nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
) -> str:
command = [evaluate_node(part, context=context) for part in nodes]
return context.executor(command, context.environment)
Expand Down
7 changes: 4 additions & 3 deletions cibuildwheel/environment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import dataclasses
from typing import Any, Mapping, Sequence
from collections.abc import Mapping, Sequence
from typing import Any

import bashlex
import bashlex.errors
Expand Down Expand Up @@ -54,7 +55,7 @@ class EnvironmentAssignment(Protocol):
def evaluated_value(
self,
*,
environment: dict[str, str],
environment: Mapping[str, str],
executor: bashlex_eval.EnvironmentExecutor | None = None,
) -> str:
"""Returns the value of this assignment, as evaluated in the environment"""
Expand Down Expand Up @@ -91,7 +92,7 @@ def __init__(self, assignment: str):

def evaluated_value(
self,
environment: dict[str, str],
environment: Mapping[str, str],
executor: bashlex_eval.EnvironmentExecutor | None = None,
) -> str:
return bashlex_eval.evaluate(self.value, environment=environment, executor=executor)
Expand Down
5 changes: 4 additions & 1 deletion cibuildwheel/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

from collections.abc import Mapping, Sequence
from io import StringIO

from .typing import Protocol
Expand All @@ -16,7 +17,9 @@ def __str__(self) -> str:
...


def dump_python_configurations(inp: dict[str, dict[str, list[dict[str, Printable]]]]) -> str:
def dump_python_configurations(
inp: Mapping[str, Mapping[str, Sequence[Mapping[str, Printable]]]]
) -> str:
output = StringIO()
for header, values in inp.items():
output.write(f"[{header}]\n")
Expand Down
8 changes: 5 additions & 3 deletions cibuildwheel/functools_cached_property_38.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import typing
from collections.abc import Callable
from threading import RLock
from typing import Any, Callable, Generic, TypeVar, overload
from typing import Any, Generic, TypeVar

__all__ = ["cached_property"]

Expand All @@ -24,11 +26,11 @@ def __set_name__(self, owner: type[Any], name: str) -> None:
msg = f"Cannot assign the same cached_property to two different names ({self.attrname!r} and {name!r})."
raise TypeError(msg)

@overload
@typing.overload
def __get__(self, instance: None, owner: type[Any] | None = ...) -> cached_property[_T]:
...

@overload
@typing.overload
def __get__(self, instance: object, owner: type[Any] | None = ...) -> _T:
...

Expand Down
10 changes: 5 additions & 5 deletions cibuildwheel/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import subprocess
import sys
import textwrap
from collections.abc import Set
from collections.abc import Iterable, Iterator, Sequence, Set
from dataclasses import dataclass
from pathlib import Path, PurePath, PurePosixPath
from typing import Iterator, Tuple
from typing import Tuple

from .architecture import Architecture
from .logger import log
Expand Down Expand Up @@ -113,7 +113,7 @@ def get_build_steps(


def check_all_python_exist(
*, platform_configs: list[PythonConfiguration], container: OCIContainer
*, platform_configs: Iterable[PythonConfiguration], container: OCIContainer
) -> None:
exist = True
messages = []
Expand All @@ -138,7 +138,7 @@ def check_all_python_exist(
def build_in_container(
*,
options: Options,
platform_configs: list[PythonConfiguration],
platform_configs: Sequence[PythonConfiguration],
container: OCIContainer,
container_project_path: PurePath,
container_package_dir: PurePath,
Expand Down Expand Up @@ -438,7 +438,7 @@ def build(options: Options, tmp_path: Path) -> None: # noqa: ARG001
sys.exit(1)


def _matches_prepared_command(error_cmd: list[str], command_template: str) -> bool:
def _matches_prepared_command(error_cmd: Sequence[str], command_template: str) -> bool:
if len(error_cmd) < 3 or error_cmd[0:2] != ["sh", "-c"]:
return False
command_prefix = command_template.split("{", maxsplit=1)[0].strip()
Expand Down
7 changes: 4 additions & 3 deletions cibuildwheel/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import shutil
import subprocess
import sys
from collections.abc import Set
import typing
from collections.abc import Sequence, Set
from dataclasses import dataclass
from pathlib import Path
from typing import Sequence, Tuple, cast
from typing import Tuple

from filelock import FileLock

Expand Down Expand Up @@ -55,7 +56,7 @@ def get_macos_version() -> tuple[int, int]:
"""
version_str, _, _ = platform.mac_ver()
version = tuple(map(int, version_str.split(".")[:2]))
return cast(Tuple[int, int], version)
return typing.cast(Tuple[int, int], version)


def get_macos_sdks() -> list[str]:
Expand Down
10 changes: 6 additions & 4 deletions cibuildwheel/oci_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import shutil
import subprocess
import sys
import typing
import uuid
from collections.abc import Mapping, Sequence
from pathlib import Path, PurePath, PurePosixPath
from types import TracebackType
from typing import IO, Dict, Sequence, cast
from typing import IO, Dict

from cibuildwheel.util import CIProvider, detect_ci_provider

Expand Down Expand Up @@ -239,7 +241,7 @@ def glob(self, path: PurePosixPath, pattern: str) -> list[PurePosixPath]:
def call(
self,
args: Sequence[PathOrStr],
env: dict[str, str] | None = None,
env: Mapping[str, str] | None = None,
capture_output: bool = False,
cwd: PathOrStr | None = None,
) -> str:
Expand Down Expand Up @@ -329,9 +331,9 @@ def get_environment(self) -> dict[str, str]:
capture_output=True,
)
)
return cast(Dict[str, str], env)
return typing.cast(Dict[str, str], env)

def environment_executor(self, command: list[str], environment: dict[str, str]) -> str:
def environment_executor(self, command: Sequence[str], environment: dict[str, str]) -> str:
# used as an EnvironmentExecutor to evaluate commands and capture output
return self.call(command, env=environment, capture_output=True)

Expand Down
Loading

0 comments on commit dfbc6c3

Please sign in to comment.