Skip to content

Commit 8c5f89c

Browse files
committed
chore: update typing to be generic on function args
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 6d038df commit 8c5f89c

12 files changed

+48
-39
lines changed

bin/inspect_all_known_projects.py

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

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

2121
import click
@@ -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

+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_pythons.py

+3-2
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

@@ -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,7 +251,7 @@ 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}.*")

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/bashlex_eval.py

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

33
import subprocess
4-
from collections.abc import Sequence
4+
from collections.abc import Iterable, Mapping, Sequence
55
from dataclasses import dataclass
66
from typing import Callable, Dict, List # noqa: TID251
77

@@ -11,7 +11,7 @@
1111
EnvironmentExecutor = Callable[[List[str], Dict[str, str]], str]
1212

1313

14-
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:
1515
return subprocess.run(command, env=env, text=True, stdout=subprocess.PIPE, check=True).stdout
1616

1717

@@ -23,7 +23,7 @@ class NodeExecutionContext:
2323

2424

2525
def evaluate(
26-
value: str, environment: dict[str, str], executor: EnvironmentExecutor | None = None
26+
value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
2727
) -> str:
2828
if not value:
2929
# empty string evaluates to empty string
@@ -41,7 +41,9 @@ def evaluate(
4141
return evaluate_node(
4242
value_word_node,
4343
context=NodeExecutionContext(
44-
environment=environment, input=value, executor=executor or local_environment_executor
44+
environment=dict(environment),
45+
input=value,
46+
executor=executor or local_environment_executor,
4547
),
4648
)
4749

@@ -106,7 +108,7 @@ def evaluate_nodes_as_compound_command(
106108

107109

108110
def evaluate_nodes_as_simple_command(
109-
nodes: list[bashlex.ast.node], context: NodeExecutionContext
111+
nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
110112
) -> str:
111113
command = [evaluate_node(part, context=context) for part in nodes]
112114
return context.executor(command, context.environment)

cibuildwheel/environment.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class EnvironmentAssignment(Protocol):
5555
def evaluated_value(
5656
self,
5757
*,
58-
environment: dict[str, str],
58+
environment: Mapping[str, str],
5959
executor: bashlex_eval.EnvironmentExecutor | None = None,
6060
) -> str:
6161
"""Returns the value of this assignment, as evaluated in the environment"""
@@ -92,7 +92,7 @@ def __init__(self, assignment: str):
9292

9393
def evaluated_value(
9494
self,
95-
environment: dict[str, str],
95+
environment: Mapping[str, str],
9696
executor: bashlex_eval.EnvironmentExecutor | None = None,
9797
) -> str:
9898
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/linux.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
import sys
55
import textwrap
6-
from collections.abc import Iterator, Set
6+
from collections.abc import Iterable, Iterator, Sequence, Set
77
from dataclasses import dataclass
88
from pathlib import Path, PurePath, PurePosixPath
99
from typing import Tuple
@@ -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/oci_container.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
import typing
1212
import uuid
13-
from collections.abc import Sequence
13+
from collections.abc import Mapping, Sequence
1414
from pathlib import Path, PurePath, PurePosixPath
1515
from types import TracebackType
1616
from typing import IO, Dict
@@ -241,7 +241,7 @@ def glob(self, path: PurePosixPath, pattern: str) -> list[PurePosixPath]:
241241
def call(
242242
self,
243243
args: Sequence[PathOrStr],
244-
env: dict[str, str] | None = None,
244+
env: Mapping[str, str] | None = None,
245245
capture_output: bool = False,
246246
cwd: PathOrStr | None = None,
247247
) -> str:
@@ -333,7 +333,7 @@ def get_environment(self) -> dict[str, str]:
333333
)
334334
return typing.cast(Dict[str, str], env)
335335

336-
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:
337337
# used as an EnvironmentExecutor to evaluate commands and capture output
338338
return self.call(command, env=environment, capture_output=True)
339339

cibuildwheel/options.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import textwrap
1212
import traceback
1313
import typing
14-
from collections.abc import Callable, Generator, Iterator, Mapping, Set
14+
from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Set
1515
from pathlib import Path
1616
from typing import Any, Dict, List, Union
1717

@@ -601,7 +601,7 @@ def build_options(self, identifier: str | None) -> BuildOptions:
601601
config_settings=config_settings,
602602
)
603603

604-
def check_for_invalid_configuration(self, identifiers: list[str]) -> None:
604+
def check_for_invalid_configuration(self, identifiers: Iterable[str]) -> None:
605605
if self.platform in {"macos", "windows"}:
606606
before_all_values = {self.build_options(i).before_all for i in identifiers}
607607

@@ -633,7 +633,7 @@ def defaults(self) -> Options:
633633
read_config_file=False,
634634
)
635635

636-
def summary(self, identifiers: list[str]) -> str:
636+
def summary(self, identifiers: Iterable[str]) -> str:
637637
lines = []
638638
global_option_names = sorted(f.name for f in dataclasses.fields(self.globals))
639639

@@ -671,7 +671,7 @@ def option_summary(
671671
option_name: str,
672672
option_value: Any,
673673
default_value: Any,
674-
overrides: dict[str, Any] | None = None,
674+
overrides: Mapping[str, Any] | None = None,
675675
) -> str:
676676
"""
677677
Return a summary of the option value, including any overrides, with

cibuildwheel/util.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import time
1414
import typing
1515
import urllib.request
16-
from collections.abc import Generator, Iterable, Sequence
16+
from collections.abc import Generator, Iterable, Mapping, Sequence
1717
from dataclasses import dataclass
1818
from enum import Enum
1919
from functools import lru_cache
@@ -102,7 +102,7 @@ def build_frontend_or_default(
102102
@typing.overload
103103
def call(
104104
*args: PathOrStr,
105-
env: dict[str, str] | None = None,
105+
env: Mapping[str, str] | None = None,
106106
cwd: PathOrStr | None = None,
107107
capture_stdout: Literal[False] = ...,
108108
) -> None:
@@ -112,7 +112,7 @@ def call(
112112
@typing.overload
113113
def call(
114114
*args: PathOrStr,
115-
env: dict[str, str] | None = None,
115+
env: Mapping[str, str] | None = None,
116116
cwd: PathOrStr | None = None,
117117
capture_stdout: Literal[True],
118118
) -> str:
@@ -121,7 +121,7 @@ def call(
121121

122122
def call(
123123
*args: PathOrStr,
124-
env: dict[str, str] | None = None,
124+
env: Mapping[str, str] | None = None,
125125
cwd: PathOrStr | None = None,
126126
capture_stdout: bool = False,
127127
) -> str | None:
@@ -144,7 +144,9 @@ def call(
144144
return typing.cast(str, result.stdout)
145145

146146

147-
def shell(*commands: str, env: dict[str, str] | None = None, cwd: PathOrStr | None = None) -> None:
147+
def shell(
148+
*commands: str, env: Mapping[str, str] | None = None, cwd: PathOrStr | None = None
149+
) -> None:
148150
command = " ".join(commands)
149151
print(f"+ {command}")
150152
subprocess.run(command, env=env, cwd=cwd, shell=True, check=True)
@@ -499,7 +501,7 @@ def print_new_wheels(msg: str, output_dir: Path) -> Generator[None, None, None]:
499501
)
500502

501503

502-
def get_pip_version(env: dict[str, str]) -> str:
504+
def get_pip_version(env: Mapping[str, str]) -> str:
503505
versions_output_text = call(
504506
"python", "-m", "pip", "freeze", "--all", capture_stdout=True, env=env
505507
)

cibuildwheel/windows.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import subprocess
77
import sys
88
import textwrap
9-
from collections.abc import Sequence, Set
9+
from collections.abc import MutableMapping, Sequence, Set
1010
from contextlib import suppress
1111
from dataclasses import dataclass
1212
from functools import lru_cache
@@ -137,7 +137,7 @@ def setup_setuptools_cross_compile(
137137
tmp: Path,
138138
python_configuration: PythonConfiguration,
139139
python_libs_base: Path,
140-
env: dict[str, str],
140+
env: MutableMapping[str, str],
141141
) -> None:
142142
distutils_cfg = tmp / "extra-setup.cfg"
143143
env["DIST_EXTRA_CONFIG"] = str(distutils_cfg)
@@ -185,7 +185,7 @@ def setup_rust_cross_compile(
185185
tmp: Path, # noqa: ARG001
186186
python_configuration: PythonConfiguration,
187187
python_libs_base: Path, # noqa: ARG001
188-
env: dict[str, str],
188+
env: MutableMapping[str, str],
189189
) -> None:
190190
# Assume that MSVC will be used, because we already know that we are
191191
# cross-compiling. MinGW users can set CARGO_BUILD_TARGET themselves

0 commit comments

Comments
 (0)