Skip to content

Commit dfbc6c3

Browse files
authored
Merge pull request #1476 from henryiii/henryiii/chore/ruff_ex
chore: ruff and typing updates
2 parents 440e18c + 8c5f89c commit dfbc6c3

19 files changed

+112
-92
lines changed

bin/inspect_all_known_projects.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from __future__ import annotations
1616

1717
import ast
18+
from collections.abc import Iterable, Iterator
1819
from pathlib import Path
19-
from typing import Iterator
2020

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

100-
def on_each(self, repos: list[str]) -> Iterator[tuple[str, str, str | None]]:
100+
def on_each(self, repos: Iterable[str]) -> Iterator[tuple[str, str, str | None]]:
101101
for repo in repos:
102102
print(f"[bold]{repo}:")
103103
for filename in sorted(self.contents, reverse=True):

bin/projects.py

100644100755
+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import textwrap
1717
import urllib.request
1818
import xml.dom.minidom
19+
from collections.abc import Iterable, Mapping, Sequence
1920
from datetime import datetime
2021
from io import StringIO
2122
from pathlib import Path
@@ -42,7 +43,7 @@
4243
class Project:
4344
NAME: int = 0
4445

45-
def __init__(self, config: dict[str, Any], github: Github | None = None):
46+
def __init__(self, config: Mapping[str, Any], github: Github | None = None):
4647
try:
4748
self.name: str = config["name"]
4849
self.gh: str = config["gh"]
@@ -149,7 +150,7 @@ def path_for_icon(icon_name: str, relative_to: Path | None = None) -> Path:
149150

150151

151152
def get_projects(
152-
config: list[dict[str, Any]],
153+
config: Iterable[Mapping[str, Any]],
153154
*,
154155
online: bool = True,
155156
auth: str | None = None,
@@ -163,7 +164,7 @@ def get_projects(
163164
return sorted((Project(item, github) for item in config), reverse=online)
164165

165166

166-
def render_projects(projects: list[Project], *, dest_path: Path, include_info: bool = True):
167+
def render_projects(projects: Sequence[Project], *, dest_path: Path, include_info: bool = True):
167168
io = StringIO()
168169
print = functools.partial(builtins.print, file=io)
169170

@@ -191,7 +192,7 @@ def render_projects(projects: list[Project], *, dest_path: Path, include_info: b
191192
def insert_projects_table(
192193
file: Path,
193194
*,
194-
projects: list[Project],
195+
projects: Sequence[Project],
195196
input_filename: str,
196197
include_info: bool = True,
197198
):

bin/update_how_it_works_image.py

100644100755
File mode changed.

bin/update_pythons.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import difflib
77
import logging
88
import sys
9+
from collections.abc import Mapping, MutableMapping
910
from pathlib import Path
1011
from typing import Any, Union
1112

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

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

9899
if not versions:
99100
return None
@@ -124,7 +125,7 @@ def __init__(self, arch_str: ArchStr):
124125
]
125126
self.arch = arch_str
126127

127-
def get_arch_file(self, release: dict[str, Any]) -> str:
128+
def get_arch_file(self, release: Mapping[str, Any]) -> str:
128129
urls: list[str] = [
129130
rf["download_url"]
130131
for rf in release["files"]
@@ -250,11 +251,11 @@ def __init__(self) -> None:
250251
self.macos_pypy = PyPyVersions("64")
251252
self.macos_pypy_arm64 = PyPyVersions("ARM64")
252253

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

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

284285
if config != orig_config:
285-
log.info(f" Updated {orig_config} to {config}")
286+
log.info(" Updated %s to %s", orig_config, config)
286287

287288

288289
@click.command()

bin/update_virtualenv.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def git_ls_remote_versions(url) -> list[VersionTuple]:
5454
try:
5555
version = Version(version_string)
5656
if version.is_devrelease:
57-
log.info(f"Ignoring development release '{version}'")
57+
log.info("Ignoring development release %r", str(version))
5858
continue
5959
if version.is_prerelease:
60-
log.info(f"Ignoring pre-release '{version}'")
60+
log.info("Ignoring pre-release %r", str(version))
6161
continue
6262
versions.append(VersionTuple(version, version_string))
6363
except InvalidVersion:
64-
log.warning(f"Ignoring ref '{ref}'")
64+
log.warning("Ignoring ref %r", ref)
6565
versions.sort(reverse=True)
6666
return versions
6767

cibuildwheel/__main__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import tarfile
88
import textwrap
99
import typing
10-
from collections.abc import Sequence, Set
10+
from collections.abc import Iterable, Sequence, Set
1111
from pathlib import Path
1212
from tempfile import mkdtemp
1313

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

339339

340-
def print_preamble(platform: str, options: Options, identifiers: list[str]) -> None:
340+
def print_preamble(platform: str, options: Options, identifiers: Sequence[str]) -> None:
341341
print(
342342
textwrap.dedent(
343343
"""
@@ -377,7 +377,7 @@ def get_build_identifiers(
377377
return [config.identifier for config in python_configurations]
378378

379379

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

383383
# warn about deprecated {python} and {pip}

cibuildwheel/architecture.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import platform as platform_module
55
import re
66
import sys
7+
from collections.abc import Set
78
from enum import Enum
89

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

133134
def allowed_architectures_check(
134135
platform: PlatformName,
135-
architectures: set[Architecture],
136+
architectures: Set[Architecture],
136137
) -> None:
137138
allowed_architectures = Architecture.all_archs(platform)
138139

cibuildwheel/bashlex_eval.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import annotations
22

33
import subprocess
4+
from collections.abc import Iterable, Mapping, Sequence
45
from dataclasses import dataclass
5-
from typing import Callable, Dict, List, Sequence
6+
from typing import Callable, Dict, List # noqa: TID251
67

78
import bashlex
89

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

1213

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

1617

@@ -22,7 +23,7 @@ class NodeExecutionContext:
2223

2324

2425
def evaluate(
25-
value: str, environment: dict[str, str], executor: EnvironmentExecutor | None = None
26+
value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
2627
) -> str:
2728
if not value:
2829
# empty string evaluates to empty string
@@ -40,7 +41,9 @@ def evaluate(
4041
return evaluate_node(
4142
value_word_node,
4243
context=NodeExecutionContext(
43-
environment=environment, input=value, executor=executor or local_environment_executor
44+
environment=dict(environment),
45+
input=value,
46+
executor=executor or local_environment_executor,
4447
),
4548
)
4649

@@ -105,7 +108,7 @@ def evaluate_nodes_as_compound_command(
105108

106109

107110
def evaluate_nodes_as_simple_command(
108-
nodes: list[bashlex.ast.node], context: NodeExecutionContext
111+
nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
109112
) -> str:
110113
command = [evaluate_node(part, context=context) for part in nodes]
111114
return context.executor(command, context.environment)

cibuildwheel/environment.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

33
import dataclasses
4-
from typing import Any, Mapping, Sequence
4+
from collections.abc import Mapping, Sequence
5+
from typing import Any
56

67
import bashlex
78
import bashlex.errors
@@ -54,7 +55,7 @@ class EnvironmentAssignment(Protocol):
5455
def evaluated_value(
5556
self,
5657
*,
57-
environment: dict[str, str],
58+
environment: Mapping[str, str],
5859
executor: bashlex_eval.EnvironmentExecutor | None = None,
5960
) -> str:
6061
"""Returns the value of this assignment, as evaluated in the environment"""
@@ -91,7 +92,7 @@ def __init__(self, assignment: str):
9192

9293
def evaluated_value(
9394
self,
94-
environment: dict[str, str],
95+
environment: Mapping[str, str],
9596
executor: bashlex_eval.EnvironmentExecutor | None = None,
9697
) -> str:
9798
return bashlex_eval.evaluate(self.value, environment=environment, executor=executor)

cibuildwheel/extra.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
from collections.abc import Mapping, Sequence
78
from io import StringIO
89

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

1819

19-
def dump_python_configurations(inp: dict[str, dict[str, list[dict[str, Printable]]]]) -> str:
20+
def dump_python_configurations(
21+
inp: Mapping[str, Mapping[str, Sequence[Mapping[str, Printable]]]]
22+
) -> str:
2023
output = StringIO()
2124
for header, values in inp.items():
2225
output.write(f"[{header}]\n")

cibuildwheel/functools_cached_property_38.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import typing
4+
from collections.abc import Callable
35
from threading import RLock
4-
from typing import Any, Callable, Generic, TypeVar, overload
6+
from typing import Any, Generic, TypeVar
57

68
__all__ = ["cached_property"]
79

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

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

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

cibuildwheel/linux.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import subprocess
44
import sys
55
import textwrap
6-
from collections.abc import Set
6+
from collections.abc import Iterable, Iterator, Sequence, Set
77
from dataclasses import dataclass
88
from pathlib import Path, PurePath, PurePosixPath
9-
from typing import Iterator, Tuple
9+
from typing import Tuple
1010

1111
from .architecture import Architecture
1212
from .logger import log
@@ -113,7 +113,7 @@ def get_build_steps(
113113

114114

115115
def check_all_python_exist(
116-
*, platform_configs: list[PythonConfiguration], container: OCIContainer
116+
*, platform_configs: Iterable[PythonConfiguration], container: OCIContainer
117117
) -> None:
118118
exist = True
119119
messages = []
@@ -138,7 +138,7 @@ def check_all_python_exist(
138138
def build_in_container(
139139
*,
140140
options: Options,
141-
platform_configs: list[PythonConfiguration],
141+
platform_configs: Sequence[PythonConfiguration],
142142
container: OCIContainer,
143143
container_project_path: PurePath,
144144
container_package_dir: PurePath,
@@ -438,7 +438,7 @@ def build(options: Options, tmp_path: Path) -> None: # noqa: ARG001
438438
sys.exit(1)
439439

440440

441-
def _matches_prepared_command(error_cmd: list[str], command_template: str) -> bool:
441+
def _matches_prepared_command(error_cmd: Sequence[str], command_template: str) -> bool:
442442
if len(error_cmd) < 3 or error_cmd[0:2] != ["sh", "-c"]:
443443
return False
444444
command_prefix = command_template.split("{", maxsplit=1)[0].strip()

cibuildwheel/macos.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import shutil
99
import subprocess
1010
import sys
11-
from collections.abc import Set
11+
import typing
12+
from collections.abc import Sequence, Set
1213
from dataclasses import dataclass
1314
from pathlib import Path
14-
from typing import Sequence, Tuple, cast
15+
from typing import Tuple
1516

1617
from filelock import FileLock
1718

@@ -55,7 +56,7 @@ def get_macos_version() -> tuple[int, int]:
5556
"""
5657
version_str, _, _ = platform.mac_ver()
5758
version = tuple(map(int, version_str.split(".")[:2]))
58-
return cast(Tuple[int, int], version)
59+
return typing.cast(Tuple[int, int], version)
5960

6061

6162
def get_macos_sdks() -> list[str]:

cibuildwheel/oci_container.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import shutil
99
import subprocess
1010
import sys
11+
import typing
1112
import uuid
13+
from collections.abc import Mapping, Sequence
1214
from pathlib import Path, PurePath, PurePosixPath
1315
from types import TracebackType
14-
from typing import IO, Dict, Sequence, cast
16+
from typing import IO, Dict
1517

1618
from cibuildwheel.util import CIProvider, detect_ci_provider
1719

@@ -239,7 +241,7 @@ def glob(self, path: PurePosixPath, pattern: str) -> list[PurePosixPath]:
239241
def call(
240242
self,
241243
args: Sequence[PathOrStr],
242-
env: dict[str, str] | None = None,
244+
env: Mapping[str, str] | None = None,
243245
capture_output: bool = False,
244246
cwd: PathOrStr | None = None,
245247
) -> str:
@@ -329,9 +331,9 @@ def get_environment(self) -> dict[str, str]:
329331
capture_output=True,
330332
)
331333
)
332-
return cast(Dict[str, str], env)
334+
return typing.cast(Dict[str, str], env)
333335

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

0 commit comments

Comments
 (0)