diff --git a/bin/inspect_all_known_projects.py b/bin/inspect_all_known_projects.py index 3f91b2b5..684b7ff1 100755 --- a/bin/inspect_all_known_projects.py +++ b/bin/inspect_all_known_projects.py @@ -15,8 +15,8 @@ the results without the `--online` setting. from __future__ import annotations import ast +from collections.abc import Iterator from pathlib import Path -from typing import Iterator import click import yaml diff --git a/cibuildwheel/architecture.py b/cibuildwheel/architecture.py index e1c8482a..8c4b2293 100644 --- a/cibuildwheel/architecture.py +++ b/cibuildwheel/architecture.py @@ -4,6 +4,7 @@ import functools 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 @@ -132,7 +133,7 @@ class Architecture(Enum): def allowed_architectures_check( platform: PlatformName, - architectures: set[Architecture], + architectures: Set[Architecture], ) -> None: allowed_architectures = Architecture.all_archs(platform) diff --git a/cibuildwheel/bashlex_eval.py b/cibuildwheel/bashlex_eval.py index 01093a62..3a167890 100644 --- a/cibuildwheel/bashlex_eval.py +++ b/cibuildwheel/bashlex_eval.py @@ -1,8 +1,9 @@ from __future__ import annotations import subprocess +from collections.abc import Sequence from dataclasses import dataclass -from typing import Callable, Dict, List, Sequence +from typing import Callable, Dict, List # noqa: TID251 import bashlex diff --git a/cibuildwheel/environment.py b/cibuildwheel/environment.py index 74bfd78a..625529e9 100644 --- a/cibuildwheel/environment.py +++ b/cibuildwheel/environment.py @@ -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 diff --git a/cibuildwheel/functools_cached_property_38.py b/cibuildwheel/functools_cached_property_38.py index 879cdb62..134564b5 100644 --- a/cibuildwheel/functools_cached_property_38.py +++ b/cibuildwheel/functools_cached_property_38.py @@ -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"] @@ -24,11 +26,11 @@ class cached_property(Generic[_T]): 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: ... diff --git a/cibuildwheel/linux.py b/cibuildwheel/linux.py index eb878d8d..37ef5c8f 100644 --- a/cibuildwheel/linux.py +++ b/cibuildwheel/linux.py @@ -3,10 +3,10 @@ from __future__ import annotations import subprocess import sys import textwrap -from collections.abc import Set +from collections.abc import Iterator, 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 diff --git a/cibuildwheel/macos.py b/cibuildwheel/macos.py index 24d22e46..75614449 100644 --- a/cibuildwheel/macos.py +++ b/cibuildwheel/macos.py @@ -8,10 +8,11 @@ import re 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 @@ -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]: diff --git a/cibuildwheel/oci_container.py b/cibuildwheel/oci_container.py index df6af6c9..8da216a2 100644 --- a/cibuildwheel/oci_container.py +++ b/cibuildwheel/oci_container.py @@ -8,10 +8,12 @@ import shlex import shutil import subprocess import sys +import typing import uuid +from collections.abc import 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 @@ -329,7 +331,7 @@ class OCIContainer: 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: # used as an EnvironmentExecutor to evaluate commands and capture output diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index 3469dce8..974470c6 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -10,8 +10,10 @@ import shlex import sys import textwrap import traceback +import typing +from collections.abc import Callable, Generator, Iterator, Mapping, Set from pathlib import Path -from typing import Any, Callable, Dict, Generator, Iterator, List, Mapping, Union, cast +from typing import Any, Dict, List, Union if sys.version_info >= (3, 11): import tomllib @@ -193,7 +195,7 @@ class OptionsReader: *, platform: PlatformName, env: Mapping[str, str], - disallow: dict[str, set[str]] | None = None, + disallow: Mapping[str, Set[str]] | None = None, ) -> None: self.platform = platform self.env = env @@ -462,7 +464,7 @@ class Options: print(msg, file=sys.stderr) sys.exit(2) - container_engine = cast(ContainerEngine, container_engine_str) + container_engine = typing.cast(ContainerEngine, container_engine_str) return GlobalOptions( package_dir=package_dir, diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index 38622c70..53cb22a6 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -11,23 +11,15 @@ import subprocess import sys import textwrap import time +import typing import urllib.request +from collections.abc import Generator, Iterable, Sequence from dataclasses import dataclass from enum import Enum from functools import lru_cache from pathlib import Path, PurePath from time import sleep -from typing import ( - Any, - ClassVar, - Generator, - Iterable, - Sequence, - TextIO, - TypeVar, - cast, - overload, -) +from typing import Any, ClassVar, TextIO, TypeVar import bracex import certifi @@ -107,7 +99,7 @@ CIBW_CACHE_PATH: Final[Path] = Path( IS_WIN: Final[bool] = sys.platform.startswith("win") -@overload +@typing.overload def call( *args: PathOrStr, env: dict[str, str] | None = None, @@ -117,7 +109,7 @@ def call( ... -@overload +@typing.overload def call( *args: PathOrStr, env: dict[str, str] | None = None, @@ -149,7 +141,7 @@ def call( result = subprocess.run(args_, check=True, shell=IS_WIN, env=env, cwd=cwd, **kwargs) if not capture_stdout: return None - return 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: diff --git a/cibuildwheel/windows.py b/cibuildwheel/windows.py index 2c0df37f..5ab8827a 100644 --- a/cibuildwheel/windows.py +++ b/cibuildwheel/windows.py @@ -6,12 +6,11 @@ import shutil import subprocess import sys import textwrap -from collections.abc import Set +from collections.abc import Sequence, Set from contextlib import suppress from dataclasses import dataclass from functools import lru_cache from pathlib import Path -from typing import Sequence from zipfile import ZipFile from filelock import FileLock diff --git a/pyproject.toml b/pyproject.toml index c71a1ae4..570772db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -136,6 +136,7 @@ select = [ "RET", # flake8-return "RUF", # Ruff-specific "SIM", # flake8-simplify + "TID251", # flake8-tidy-imports.banned-api "UP", # pyupgrade "YTT", # flake8-2020 "EXE", # flake8-executable @@ -151,6 +152,12 @@ target-version = "py37" typing-modules = ["cibuildwheel.typing"] flake8-unused-arguments.ignore-variadic-names = true +[tool.ruff.flake8-tidy-imports.banned-api] +"typing.Mapping".msg = "Use collections.abc.Mapping instead." +"typing.Callable".msg = "Use collections.abc.Callable instead." +"typing.Iterator".msg = "Use collections.abc.Iterator instead." +"typing.Sequence".msg = "Use collections.abc.Sequence instead." +"typing.Set".msg = "Use collections.abc.Set instead." [tool.ruff.per-file-ignores] "unit_test/*" = ["PLC1901"] diff --git a/unit_test/option_prepare_test.py b/unit_test/option_prepare_test.py index 5cf58569..b5c361b2 100644 --- a/unit_test/option_prepare_test.py +++ b/unit_test/option_prepare_test.py @@ -3,9 +3,9 @@ from __future__ import annotations import platform as platform_module import subprocess import sys +import typing from contextlib import contextmanager from pathlib import PurePosixPath -from typing import cast from unittest import mock import pytest @@ -52,7 +52,7 @@ def test_build_default_launches(monkeypatch): main() - build_in_container = cast(mock.Mock, linux.build_in_container) + build_in_container = typing.cast(mock.Mock, linux.build_in_container) assert build_in_container.call_count == 4 @@ -120,7 +120,7 @@ before-all = "true" main() - build_in_container = cast(mock.Mock, linux.build_in_container) + build_in_container = typing.cast(mock.Mock, linux.build_in_container) assert build_in_container.call_count == 6