Skip to content

Commit

Permalink
chore: add structurally typed GenericPythonConfiguration (pypa#1458)
Browse files Browse the repository at this point in the history
* Add structually typed GenericPythonConfiguration

Use structural typing for the return value of get_python_configurations
so that we don't have to specify a union of all possible PythonConfiguration
types

* address henryiii's comments

* Use Set in more places
  • Loading branch information
hoodmane committed Apr 6, 2023
1 parent bd5294b commit d018570
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
16 changes: 9 additions & 7 deletions cibuildwheel/__main__.py
Expand Up @@ -7,6 +7,7 @@
import tarfile
import textwrap
import typing
from collections.abc import Sequence, Set
from pathlib import Path
from tempfile import mkdtemp

Expand All @@ -18,7 +19,12 @@
from cibuildwheel.architecture import Architecture, allowed_architectures_check
from cibuildwheel.logger import log
from cibuildwheel.options import CommandLineArguments, Options, compute_options
from cibuildwheel.typing import PLATFORMS, PlatformName, assert_never
from cibuildwheel.typing import (
PLATFORMS,
GenericPythonConfiguration,
PlatformName,
assert_never,
)
from cibuildwheel.util import (
CIBW_CACHE_PATH,
BuildSelector,
Expand Down Expand Up @@ -339,13 +345,9 @@ def print_preamble(platform: str, options: Options, identifiers: list[str]) -> N


def get_build_identifiers(
platform: PlatformName, build_selector: BuildSelector, architectures: set[Architecture]
platform: PlatformName, build_selector: BuildSelector, architectures: Set[Architecture]
) -> list[str]:
python_configurations: (
list[cibuildwheel.linux.PythonConfiguration]
| list[cibuildwheel.windows.PythonConfiguration]
| list[cibuildwheel.macos.PythonConfiguration]
)
python_configurations: Sequence[GenericPythonConfiguration]

if platform == "linux":
python_configurations = cibuildwheel.linux.get_python_configurations(
Expand Down
3 changes: 2 additions & 1 deletion cibuildwheel/linux.py
Expand Up @@ -3,6 +3,7 @@
import subprocess
import sys
import textwrap
from collections.abc import Set
from dataclasses import dataclass
from pathlib import Path, PurePath, PurePosixPath
from typing import Iterator, Tuple
Expand Down Expand Up @@ -46,7 +47,7 @@ class BuildStep:

def get_python_configurations(
build_selector: BuildSelector,
architectures: set[Architecture],
architectures: Set[Architecture],
) -> list[PythonConfiguration]:
full_python_configs = read_python_configs("linux")

Expand Down
3 changes: 2 additions & 1 deletion cibuildwheel/macos.py
Expand Up @@ -8,6 +8,7 @@
import shutil
import subprocess
import sys
from collections.abc import Set
from dataclasses import dataclass
from pathlib import Path
from typing import Sequence, Tuple, cast
Expand Down Expand Up @@ -69,7 +70,7 @@ class PythonConfiguration:


def get_python_configurations(
build_selector: BuildSelector, architectures: set[Architecture]
build_selector: BuildSelector, architectures: Set[Architecture]
) -> list[PythonConfiguration]:
full_python_configs = read_python_configs("macos")

Expand Down
6 changes: 6 additions & 0 deletions cibuildwheel/typing.py
Expand Up @@ -42,3 +42,9 @@

PlatformName = Literal["linux", "macos", "windows"]
PLATFORMS: Final[set[PlatformName]] = {"linux", "macos", "windows"}


class GenericPythonConfiguration(Protocol):
@property
def identifier(self) -> str:
...
3 changes: 2 additions & 1 deletion cibuildwheel/windows.py
Expand Up @@ -6,6 +6,7 @@
import subprocess
import sys
import textwrap
from collections.abc import Set
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -71,7 +72,7 @@ class PythonConfiguration:

def get_python_configurations(
build_selector: BuildSelector,
architectures: set[Architecture],
architectures: Set[Architecture],
) -> list[PythonConfiguration]:
full_python_configs = read_python_configs("windows")

Expand Down

0 comments on commit d018570

Please sign in to comment.