Merge pull request #1099 from henryiii/henryiii/refactor/namespace
refactor: use dataclasses instead of NamedTuples
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import configparser
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
import requests
|
||||
|
||||
@@ -11,7 +11,8 @@ DIR = Path(__file__).parent.resolve()
|
||||
RESOURCES = DIR.parent / "cibuildwheel/resources"
|
||||
|
||||
|
||||
class Image(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class Image:
|
||||
manylinux_version: str
|
||||
platform: str
|
||||
image_name: str
|
||||
|
||||
@@ -6,8 +6,8 @@ import difflib
|
||||
import logging
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
import click
|
||||
import rich
|
||||
@@ -36,7 +36,8 @@ GET_VIRTUALENV_URL_TEMPLATE: Final[
|
||||
] = f"{GET_VIRTUALENV_GITHUB}/blob/{{version}}/public/virtualenv.pyz?raw=true"
|
||||
|
||||
|
||||
class VersionTuple(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class VersionTuple:
|
||||
version: Version
|
||||
version_string: str
|
||||
|
||||
|
||||
@@ -116,7 +116,7 @@ def main() -> None:
|
||||
help="Enable pre-release Python versions if available.",
|
||||
)
|
||||
|
||||
args = parser.parse_args(namespace=CommandLineArguments())
|
||||
args = CommandLineArguments(**vars(parser.parse_args()))
|
||||
|
||||
args.package_dir = args.package_dir.resolve()
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import subprocess
|
||||
from typing import Callable, Dict, List, NamedTuple, Optional, Sequence
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable, Dict, List, Optional, Sequence
|
||||
|
||||
import bashlex
|
||||
|
||||
@@ -13,7 +14,8 @@ def local_environment_executor(command: List[str], env: Dict[str, str]) -> str:
|
||||
).stdout
|
||||
|
||||
|
||||
class NodeExecutionContext(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class NodeExecutionContext:
|
||||
environment: Dict[str, str]
|
||||
input: str
|
||||
executor: EnvironmentExecutor
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path, PurePath, PurePosixPath
|
||||
from typing import Iterator, List, NamedTuple, Set, Tuple
|
||||
from typing import Iterator, List, Set, Tuple
|
||||
|
||||
from .architecture import Architecture
|
||||
from .docker_container import DockerContainer
|
||||
@@ -19,7 +20,8 @@ from .util import (
|
||||
)
|
||||
|
||||
|
||||
class PythonConfiguration(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class PythonConfiguration:
|
||||
version: str
|
||||
identifier: str
|
||||
path_str: str
|
||||
@@ -29,7 +31,8 @@ class PythonConfiguration(NamedTuple):
|
||||
return PurePosixPath(self.path_str)
|
||||
|
||||
|
||||
class BuildStep(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class BuildStep:
|
||||
platform_configs: List[PythonConfiguration]
|
||||
platform_tag: str
|
||||
docker_image: str
|
||||
|
||||
@@ -5,8 +5,9 @@ import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, NamedTuple, Sequence, Set, Tuple, cast
|
||||
from typing import Dict, List, Sequence, Set, Tuple, cast
|
||||
|
||||
from filelock import FileLock
|
||||
|
||||
@@ -54,7 +55,8 @@ def get_macos_sdks() -> List[str]:
|
||||
return [m.group(1) for m in re.finditer(r"-sdk (macosx\S+)", output)]
|
||||
|
||||
|
||||
class PythonConfiguration(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class PythonConfiguration:
|
||||
version: str
|
||||
identifier: str
|
||||
url: str
|
||||
|
||||
+12
-18
@@ -4,19 +4,9 @@ import sys
|
||||
import traceback
|
||||
from configparser import ConfigParser
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import asdict, dataclass
|
||||
from pathlib import Path
|
||||
from typing import (
|
||||
Any,
|
||||
Dict,
|
||||
Generator,
|
||||
List,
|
||||
Mapping,
|
||||
NamedTuple,
|
||||
Optional,
|
||||
Set,
|
||||
Tuple,
|
||||
Union,
|
||||
)
|
||||
from typing import Any, Dict, Generator, List, Mapping, Optional, Set, Tuple, Union
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
import tomllib
|
||||
@@ -45,6 +35,7 @@ from .util import (
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CommandLineArguments:
|
||||
platform: Literal["auto", "linux", "macos", "windows"]
|
||||
archs: Optional[str]
|
||||
@@ -56,7 +47,8 @@ class CommandLineArguments:
|
||||
prerelease_pythons: bool
|
||||
|
||||
|
||||
class GlobalOptions(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class GlobalOptions:
|
||||
package_dir: Path
|
||||
output_dir: Path
|
||||
build_selector: BuildSelector
|
||||
@@ -64,7 +56,8 @@ class GlobalOptions(NamedTuple):
|
||||
architectures: Set[Architecture]
|
||||
|
||||
|
||||
class BuildOptions(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class BuildOptions:
|
||||
globals: GlobalOptions
|
||||
environment: ParsedEnvironment
|
||||
before_all: str
|
||||
@@ -104,7 +97,8 @@ class BuildOptions(NamedTuple):
|
||||
Setting = Union[Dict[str, str], List[str], str, int]
|
||||
|
||||
|
||||
class Override(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class Override:
|
||||
select_pattern: str
|
||||
options: Dict[str, Setting]
|
||||
|
||||
@@ -548,12 +542,12 @@ class Options:
|
||||
def summary(self, identifiers: List[str]) -> str:
|
||||
lines = [
|
||||
f"{option_name}: {option_value!r}"
|
||||
for option_name, option_value in sorted(self.globals._asdict().items())
|
||||
for option_name, option_value in sorted(asdict(self.globals).items())
|
||||
]
|
||||
|
||||
build_option_defaults = self.build_options(identifier=None)
|
||||
|
||||
for option_name, default_value in sorted(build_option_defaults._asdict().items()):
|
||||
for option_name, default_value in sorted(asdict(build_option_defaults).items()):
|
||||
if option_name == "globals":
|
||||
continue
|
||||
|
||||
@@ -561,7 +555,7 @@ class Options:
|
||||
|
||||
# if any identifiers have an overridden value, print that too
|
||||
for identifier in identifiers:
|
||||
option_value = self.build_options(identifier=identifier)._asdict()[option_name]
|
||||
option_value = getattr(self.build_options(identifier=identifier), option_name)
|
||||
if option_value != default_value:
|
||||
lines.append(f" {identifier}: {option_value!r}")
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import fnmatch
|
||||
import itertools
|
||||
import os
|
||||
@@ -11,6 +10,7 @@ import sys
|
||||
import textwrap
|
||||
import time
|
||||
import urllib.request
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from functools import lru_cache
|
||||
from pathlib import Path, PurePath
|
||||
@@ -22,7 +22,6 @@ from typing import (
|
||||
Generator,
|
||||
Iterable,
|
||||
List,
|
||||
NamedTuple,
|
||||
Optional,
|
||||
Sequence,
|
||||
TextIO,
|
||||
@@ -233,7 +232,7 @@ def selector_matches(patterns: str, string: str) -> bool:
|
||||
|
||||
|
||||
# Once we require Python 3.10+, we can add kw_only=True
|
||||
@dataclasses.dataclass
|
||||
@dataclass(frozen=True)
|
||||
class BuildSelector:
|
||||
"""
|
||||
This class holds a set of build/skip patterns. You call an instance with a
|
||||
@@ -270,7 +269,7 @@ class BuildSelector:
|
||||
return should_build and not should_skip
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
@dataclass(frozen=True)
|
||||
class TestSelector:
|
||||
"""
|
||||
A build selector that can only skip tests according to a skip pattern.
|
||||
@@ -418,6 +417,12 @@ def unwrap(text: str) -> str:
|
||||
return re.sub(r"\s+", " ", text)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FileReport:
|
||||
name: str
|
||||
size: str
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def print_new_wheels(msg: str, output_dir: Path) -> Generator[None, None, None]:
|
||||
"""
|
||||
@@ -432,10 +437,6 @@ def print_new_wheels(msg: str, output_dir: Path) -> Generator[None, None, None]:
|
||||
yield
|
||||
final_contents = set(output_dir.iterdir())
|
||||
|
||||
class FileReport(NamedTuple):
|
||||
name: str
|
||||
size: str
|
||||
|
||||
new_contents = [
|
||||
FileReport(wheel.name, f"{(wheel.stat().st_size + 1023) // 1024:,d}")
|
||||
for wheel in final_contents - existing_contents
|
||||
|
||||
@@ -2,9 +2,10 @@ import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, NamedTuple, Optional, Sequence, Set
|
||||
from typing import Dict, List, Optional, Sequence, Set
|
||||
from zipfile import ZipFile
|
||||
|
||||
from filelock import FileLock
|
||||
@@ -46,7 +47,8 @@ def get_nuget_args(version: str, arch: str, output_directory: Path) -> List[str]
|
||||
]
|
||||
|
||||
|
||||
class PythonConfiguration(NamedTuple):
|
||||
@dataclass(frozen=True)
|
||||
class PythonConfiguration:
|
||||
version: str
|
||||
arch: str
|
||||
identifier: str
|
||||
|
||||
+10
-10
@@ -4,15 +4,15 @@ from cibuildwheel.options import CommandLineArguments
|
||||
|
||||
|
||||
def get_default_command_line_arguments() -> CommandLineArguments:
|
||||
defaults = CommandLineArguments()
|
||||
|
||||
defaults.platform = "auto"
|
||||
defaults.allow_empty = False
|
||||
defaults.archs = None
|
||||
defaults.config_file = ""
|
||||
defaults.output_dir = Path("wheelhouse")
|
||||
defaults.package_dir = Path(".")
|
||||
defaults.prerelease_pythons = False
|
||||
defaults.print_build_identifiers = False
|
||||
defaults = CommandLineArguments(
|
||||
platform="auto",
|
||||
allow_empty=False,
|
||||
archs=None,
|
||||
config_file="",
|
||||
output_dir=Path("wheelhouse"),
|
||||
package_dir=Path("."),
|
||||
prerelease_pythons=False,
|
||||
print_build_identifiers=False,
|
||||
)
|
||||
|
||||
return defaults
|
||||
|
||||
Reference in New Issue
Block a user