chore: update typing to be generic on function args

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
Henry Schreiner
2023-04-18 13:06:17 -04:00
parent 6d038dfbdf
commit 8c5f89c035
12 changed files with 48 additions and 39 deletions
+2 -2
View File
@@ -15,7 +15,7 @@ the results without the `--online` setting.
from __future__ import annotations from __future__ import annotations
import ast import ast
from collections.abc import Iterator from collections.abc import Iterable, Iterator
from pathlib import Path from pathlib import Path
import click import click
@@ -97,7 +97,7 @@ class MaybeRemote:
with open(filename, "w") as f: with open(filename, "w") as f:
yaml.safe_dump(self.contents, f, default_flow_style=False) 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: for repo in repos:
print(f"[bold]{repo}:") print(f"[bold]{repo}:")
for filename in sorted(self.contents, reverse=True): for filename in sorted(self.contents, reverse=True):
+5 -4
View File
@@ -16,6 +16,7 @@ import functools
import textwrap import textwrap
import urllib.request import urllib.request
import xml.dom.minidom import xml.dom.minidom
from collections.abc import Iterable, Mapping, Sequence
from datetime import datetime from datetime import datetime
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
@@ -42,7 +43,7 @@ ICONS = (
class Project: class Project:
NAME: int = 0 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: try:
self.name: str = config["name"] self.name: str = config["name"]
self.gh: str = config["gh"] self.gh: str = config["gh"]
@@ -149,7 +150,7 @@ def path_for_icon(icon_name: str, relative_to: Path | None = None) -> Path:
def get_projects( def get_projects(
config: list[dict[str, Any]], config: Iterable[Mapping[str, Any]],
*, *,
online: bool = True, online: bool = True,
auth: str | None = None, auth: str | None = None,
@@ -163,7 +164,7 @@ def get_projects(
return sorted((Project(item, github) for item in config), reverse=online) 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() io = StringIO()
print = functools.partial(builtins.print, file=io) print = functools.partial(builtins.print, file=io)
@@ -191,7 +192,7 @@ def render_projects(projects: list[Project], *, dest_path: Path, include_info: b
def insert_projects_table( def insert_projects_table(
file: Path, file: Path,
*, *,
projects: list[Project], projects: Sequence[Project],
input_filename: str, input_filename: str,
include_info: bool = True, include_info: bool = True,
): ):
+3 -2
View File
@@ -6,6 +6,7 @@ import copy
import difflib import difflib
import logging import logging
import sys import sys
from collections.abc import Mapping, MutableMapping
from pathlib import Path from pathlib import Path
from typing import Any, Union from typing import Any, Union
@@ -124,7 +125,7 @@ class PyPyVersions:
] ]
self.arch = arch_str 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] = [ urls: list[str] = [
rf["download_url"] rf["download_url"]
for rf in release["files"] for rf in release["files"]
@@ -250,7 +251,7 @@ class AllVersions:
self.macos_pypy = PyPyVersions("64") self.macos_pypy = PyPyVersions("64")
self.macos_pypy_arm64 = PyPyVersions("ARM64") 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"] identifier = config["identifier"]
version = Version(config["version"]) version = Version(config["version"])
spec = Specifier(f"=={version.major}.{version.minor}.*") spec = Specifier(f"=={version.major}.{version.minor}.*")
+3 -3
View File
@@ -7,7 +7,7 @@ import sys
import tarfile import tarfile
import textwrap import textwrap
import typing import typing
from collections.abc import Sequence, Set from collections.abc import Iterable, Sequence, Set
from pathlib import Path from pathlib import Path
from tempfile import mkdtemp from tempfile import mkdtemp
@@ -337,7 +337,7 @@ def build_in_directory(args: CommandLineArguments) -> None:
log.warning(f"Can't delete temporary folder '{tmp_path}'") 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( print(
textwrap.dedent( textwrap.dedent(
""" """
@@ -377,7 +377,7 @@ def get_build_identifiers(
return [config.identifier for config in python_configurations] 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 = [] warnings = []
# warn about deprecated {python} and {pip} # warn about deprecated {python} and {pip}
+7 -5
View File
@@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import subprocess import subprocess
from collections.abc import Sequence from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass from dataclasses import dataclass
from typing import Callable, Dict, List # noqa: TID251 from typing import Callable, Dict, List # noqa: TID251
@@ -11,7 +11,7 @@ import bashlex
EnvironmentExecutor = Callable[[List[str], Dict[str, str]], str] 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 return subprocess.run(command, env=env, text=True, stdout=subprocess.PIPE, check=True).stdout
@@ -23,7 +23,7 @@ class NodeExecutionContext:
def evaluate( def evaluate(
value: str, environment: dict[str, str], executor: EnvironmentExecutor | None = None value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
) -> str: ) -> str:
if not value: if not value:
# empty string evaluates to empty string # empty string evaluates to empty string
@@ -41,7 +41,9 @@ def evaluate(
return evaluate_node( return evaluate_node(
value_word_node, value_word_node,
context=NodeExecutionContext( context=NodeExecutionContext(
environment=environment, input=value, executor=executor or local_environment_executor environment=dict(environment),
input=value,
executor=executor or local_environment_executor,
), ),
) )
@@ -106,7 +108,7 @@ def evaluate_nodes_as_compound_command(
def evaluate_nodes_as_simple_command( def evaluate_nodes_as_simple_command(
nodes: list[bashlex.ast.node], context: NodeExecutionContext nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
) -> str: ) -> str:
command = [evaluate_node(part, context=context) for part in nodes] command = [evaluate_node(part, context=context) for part in nodes]
return context.executor(command, context.environment) return context.executor(command, context.environment)
+2 -2
View File
@@ -55,7 +55,7 @@ class EnvironmentAssignment(Protocol):
def evaluated_value( def evaluated_value(
self, self,
*, *,
environment: dict[str, str], environment: Mapping[str, str],
executor: bashlex_eval.EnvironmentExecutor | None = None, executor: bashlex_eval.EnvironmentExecutor | None = None,
) -> str: ) -> str:
"""Returns the value of this assignment, as evaluated in the environment""" """Returns the value of this assignment, as evaluated in the environment"""
@@ -92,7 +92,7 @@ class EnvironmentAssignmentBash:
def evaluated_value( def evaluated_value(
self, self,
environment: dict[str, str], environment: Mapping[str, str],
executor: bashlex_eval.EnvironmentExecutor | None = None, executor: bashlex_eval.EnvironmentExecutor | None = None,
) -> str: ) -> str:
return bashlex_eval.evaluate(self.value, environment=environment, executor=executor) return bashlex_eval.evaluate(self.value, environment=environment, executor=executor)
+4 -1
View File
@@ -4,6 +4,7 @@ These are utilities for the `/bin` scripts, not for the `cibuildwheel` program.
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping, Sequence
from io import StringIO from io import StringIO
from .typing import Protocol from .typing import Protocol
@@ -16,7 +17,9 @@ class Printable(Protocol):
... ...
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() output = StringIO()
for header, values in inp.items(): for header, values in inp.items():
output.write(f"[{header}]\n") output.write(f"[{header}]\n")
+4 -4
View File
@@ -3,7 +3,7 @@ from __future__ import annotations
import subprocess import subprocess
import sys import sys
import textwrap import textwrap
from collections.abc import Iterator, Set from collections.abc import Iterable, Iterator, Sequence, Set
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path, PurePath, PurePosixPath from pathlib import Path, PurePath, PurePosixPath
from typing import Tuple from typing import Tuple
@@ -113,7 +113,7 @@ def get_build_steps(
def check_all_python_exist( def check_all_python_exist(
*, platform_configs: list[PythonConfiguration], container: OCIContainer *, platform_configs: Iterable[PythonConfiguration], container: OCIContainer
) -> None: ) -> None:
exist = True exist = True
messages = [] messages = []
@@ -138,7 +138,7 @@ def check_all_python_exist(
def build_in_container( def build_in_container(
*, *,
options: Options, options: Options,
platform_configs: list[PythonConfiguration], platform_configs: Sequence[PythonConfiguration],
container: OCIContainer, container: OCIContainer,
container_project_path: PurePath, container_project_path: PurePath,
container_package_dir: PurePath, container_package_dir: PurePath,
@@ -438,7 +438,7 @@ def build(options: Options, tmp_path: Path) -> None: # noqa: ARG001
sys.exit(1) 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"]: if len(error_cmd) < 3 or error_cmd[0:2] != ["sh", "-c"]:
return False return False
command_prefix = command_template.split("{", maxsplit=1)[0].strip() command_prefix = command_template.split("{", maxsplit=1)[0].strip()
+3 -3
View File
@@ -10,7 +10,7 @@ import subprocess
import sys import sys
import typing import typing
import uuid import uuid
from collections.abc import Sequence from collections.abc import Mapping, Sequence
from pathlib import Path, PurePath, PurePosixPath from pathlib import Path, PurePath, PurePosixPath
from types import TracebackType from types import TracebackType
from typing import IO, Dict from typing import IO, Dict
@@ -241,7 +241,7 @@ class OCIContainer:
def call( def call(
self, self,
args: Sequence[PathOrStr], args: Sequence[PathOrStr],
env: dict[str, str] | None = None, env: Mapping[str, str] | None = None,
capture_output: bool = False, capture_output: bool = False,
cwd: PathOrStr | None = None, cwd: PathOrStr | None = None,
) -> str: ) -> str:
@@ -333,7 +333,7 @@ class OCIContainer:
) )
return typing.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 # used as an EnvironmentExecutor to evaluate commands and capture output
return self.call(command, env=environment, capture_output=True) return self.call(command, env=environment, capture_output=True)
+4 -4
View File
@@ -11,7 +11,7 @@ import sys
import textwrap import textwrap
import traceback import traceback
import typing import typing
from collections.abc import Callable, Generator, Iterator, Mapping, Set from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Set
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Union from typing import Any, Dict, List, Union
@@ -601,7 +601,7 @@ class Options:
config_settings=config_settings, config_settings=config_settings,
) )
def check_for_invalid_configuration(self, identifiers: list[str]) -> None: def check_for_invalid_configuration(self, identifiers: Iterable[str]) -> None:
if self.platform in {"macos", "windows"}: if self.platform in {"macos", "windows"}:
before_all_values = {self.build_options(i).before_all for i in identifiers} before_all_values = {self.build_options(i).before_all for i in identifiers}
@@ -633,7 +633,7 @@ class Options:
read_config_file=False, read_config_file=False,
) )
def summary(self, identifiers: list[str]) -> str: def summary(self, identifiers: Iterable[str]) -> str:
lines = [] lines = []
global_option_names = sorted(f.name for f in dataclasses.fields(self.globals)) global_option_names = sorted(f.name for f in dataclasses.fields(self.globals))
@@ -671,7 +671,7 @@ class Options:
option_name: str, option_name: str,
option_value: Any, option_value: Any,
default_value: Any, default_value: Any,
overrides: dict[str, Any] | None = None, overrides: Mapping[str, Any] | None = None,
) -> str: ) -> str:
""" """
Return a summary of the option value, including any overrides, with Return a summary of the option value, including any overrides, with
+8 -6
View File
@@ -13,7 +13,7 @@ import textwrap
import time import time
import typing import typing
import urllib.request import urllib.request
from collections.abc import Generator, Iterable, Sequence from collections.abc import Generator, Iterable, Mapping, Sequence
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
from functools import lru_cache from functools import lru_cache
@@ -102,7 +102,7 @@ IS_WIN: Final[bool] = sys.platform.startswith("win")
@typing.overload @typing.overload
def call( def call(
*args: PathOrStr, *args: PathOrStr,
env: dict[str, str] | None = None, env: Mapping[str, str] | None = None,
cwd: PathOrStr | None = None, cwd: PathOrStr | None = None,
capture_stdout: Literal[False] = ..., capture_stdout: Literal[False] = ...,
) -> None: ) -> None:
@@ -112,7 +112,7 @@ def call(
@typing.overload @typing.overload
def call( def call(
*args: PathOrStr, *args: PathOrStr,
env: dict[str, str] | None = None, env: Mapping[str, str] | None = None,
cwd: PathOrStr | None = None, cwd: PathOrStr | None = None,
capture_stdout: Literal[True], capture_stdout: Literal[True],
) -> str: ) -> str:
@@ -121,7 +121,7 @@ def call(
def call( def call(
*args: PathOrStr, *args: PathOrStr,
env: dict[str, str] | None = None, env: Mapping[str, str] | None = None,
cwd: PathOrStr | None = None, cwd: PathOrStr | None = None,
capture_stdout: bool = False, capture_stdout: bool = False,
) -> str | None: ) -> str | None:
@@ -144,7 +144,9 @@ def call(
return typing.cast(str, result.stdout) return typing.cast(str, result.stdout)
def shell(*commands: str, env: dict[str, str] | None = None, cwd: PathOrStr | None = None) -> None: def shell(
*commands: str, env: Mapping[str, str] | None = None, cwd: PathOrStr | None = None
) -> None:
command = " ".join(commands) command = " ".join(commands)
print(f"+ {command}") print(f"+ {command}")
subprocess.run(command, env=env, cwd=cwd, shell=True, check=True) 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]:
) )
def get_pip_version(env: dict[str, str]) -> str: def get_pip_version(env: Mapping[str, str]) -> str:
versions_output_text = call( versions_output_text = call(
"python", "-m", "pip", "freeze", "--all", capture_stdout=True, env=env "python", "-m", "pip", "freeze", "--all", capture_stdout=True, env=env
) )
+3 -3
View File
@@ -6,7 +6,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import textwrap import textwrap
from collections.abc import Sequence, Set from collections.abc import MutableMapping, Sequence, Set
from contextlib import suppress from contextlib import suppress
from dataclasses import dataclass from dataclasses import dataclass
from functools import lru_cache from functools import lru_cache
@@ -137,7 +137,7 @@ def setup_setuptools_cross_compile(
tmp: Path, tmp: Path,
python_configuration: PythonConfiguration, python_configuration: PythonConfiguration,
python_libs_base: Path, python_libs_base: Path,
env: dict[str, str], env: MutableMapping[str, str],
) -> None: ) -> None:
distutils_cfg = tmp / "extra-setup.cfg" distutils_cfg = tmp / "extra-setup.cfg"
env["DIST_EXTRA_CONFIG"] = str(distutils_cfg) env["DIST_EXTRA_CONFIG"] = str(distutils_cfg)
@@ -185,7 +185,7 @@ def setup_rust_cross_compile(
tmp: Path, # noqa: ARG001 tmp: Path, # noqa: ARG001
python_configuration: PythonConfiguration, python_configuration: PythonConfiguration,
python_libs_base: Path, # noqa: ARG001 python_libs_base: Path, # noqa: ARG001
env: dict[str, str], env: MutableMapping[str, str],
) -> None: ) -> None:
# Assume that MSVC will be used, because we already know that we are # Assume that MSVC will be used, because we already know that we are
# cross-compiling. MinGW users can set CARGO_BUILD_TARGET themselves # cross-compiling. MinGW users can set CARGO_BUILD_TARGET themselves