Skip to content

[WIP] Tighten file-api dataclasses #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/scikit_build_core/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
from typing import TYPE_CHECKING, Any

from . import __version__
from ._compat.builtins import ExceptionGroup
from ._logging import logger
from ._shutil import Run
from .errors import CMakeConfigError, CMakeNotFoundError, FailedLiveProcessError
from .file_api.query import stateless_query
from .file_api.reply import load_reply_dir
from .program_search import Program, best_program, get_cmake_program, get_cmake_programs

if TYPE_CHECKING:
Expand All @@ -25,6 +28,7 @@
from packaging.version import Version

from ._compat.typing import Self
from .file_api.model.index import Index

__all__ = ["CMake", "CMaker"]

Expand Down Expand Up @@ -83,6 +87,8 @@
init_cache_file: Path = dataclasses.field(init=False, default=Path())
env: dict[str, str] = dataclasses.field(init=False, default_factory=os.environ.copy)
single_config: bool = not sysconfig.get_platform().startswith("win")
file_api: Index | None = None
_file_api_query: Path = dataclasses.field(init=False)

def __post_init__(self) -> None:
self.init_cache_file = self.build_dir / "CMakeInit.txt"
Expand All @@ -97,6 +103,8 @@
msg = f"build directory {self.build_dir} must be a (creatable) directory"
raise CMakeConfigError(msg)

# TODO: This could be stateful instead
self._file_api_query = stateless_query(self.build_dir)
skbuild_info = self.build_dir / ".skbuild-info.json"
stale = False

Expand Down Expand Up @@ -253,6 +261,13 @@
msg = "CMake configuration failed"
raise FailedLiveProcessError(msg) from None

try:
if self._file_api_query.exists():
self.file_api = load_reply_dir(self._file_api_query)
except ExceptionGroup:
logger.error("Could not parse CMake file-api")
raise

Check warning on line 269 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L267-L269

Added lines #L267 - L269 were not covered by tests

def _compute_build_args(
self,
*,
Expand Down
26 changes: 25 additions & 1 deletion src/scikit_build_core/file_api/_cattrs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import builtins
import json
from importlib.metadata import version
from pathlib import Path
from typing import Any, Callable, Dict, Type, TypeVar # noqa: TID251
from typing import Any, Callable, Dict, Type, TypeVar, Union # noqa: TID251

import cattr
import cattr.preconf.json
from cattrs import ClassValidationError
from packaging.version import Version

from .._compat.typing import get_args
from .model.cache import Cache
from .model.cmakefiles import CMakeFiles
from .model.codemodel import CodeModel, Target
from .model.codemodel import Directory as CodeModelDirectory
from .model.common import Paths
from .model.directory import Directory
from .model.index import Index, Reply

T = TypeVar("T")
Expand All @@ -37,16 +44,33 @@
converter.register_structure_hook(Reply, st_hook)

def from_json_file(with_path: Dict[str, Any], t: Type[T]) -> T:
if "jsonFile" not in with_path and t is CodeModelDirectory:
return converter.structure_attrs_fromdict(with_path, t)

Check warning on line 48 in src/scikit_build_core/file_api/_cattrs_converter.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/file_api/_cattrs_converter.py#L48

Added line #L48 was not covered by tests
if with_path["jsonFile"] is None:
return converter.structure_attrs_fromdict({}, t)
path = base_dir / Path(with_path["jsonFile"])
raw = json.loads(path.read_text(encoding="utf-8"))
if t is CodeModelDirectory:
t = Directory # type: ignore[assignment]
return converter.structure_attrs_fromdict(raw, t)

def from_union(obj: Dict[str, Any], t: Type[T]) -> T:
for try_type in get_args(t):
try:
return converter.structure(obj, try_type) # type: ignore[no-any-return]
except ClassValidationError: # noqa: PERF203
continue
msg = f"Could not convert {obj} into {t}"
raise TypeError(msg)

Check warning on line 64 in src/scikit_build_core/file_api/_cattrs_converter.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/file_api/_cattrs_converter.py#L58-L64

Added lines #L58 - L64 were not covered by tests

converter.register_structure_hook(CodeModel, from_json_file)
converter.register_structure_hook(Target, from_json_file)
converter.register_structure_hook(Cache, from_json_file)
converter.register_structure_hook(CMakeFiles, from_json_file)
converter.register_structure_hook(CodeModelDirectory, from_json_file)
# Workaround for cattrs < 23.2.0 not handling Union with dataclass properly
if Version(version("cattrs")) < Version("23.2.0"):
converter.register_structure_hook(Union[str, Paths], from_union)

Check warning on line 73 in src/scikit_build_core/file_api/_cattrs_converter.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/file_api/_cattrs_converter.py#L73

Added line #L73 was not covered by tests
return converter


Expand Down
2 changes: 1 addition & 1 deletion src/scikit_build_core/file_api/model/codemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Sysroot:
@dataclasses.dataclass(frozen=True)
class Link:
language: str
commandFragments: List[CommandFragment]
commandFragments: List[CommandFragment] = dataclasses.field(default_factory=list)
lto: Optional[bool] = None
sysroot: Optional[Sysroot] = None

Expand Down
7 changes: 6 additions & 1 deletion src/scikit_build_core/file_api/reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .model.cache import Cache
from .model.cmakefiles import CMakeFiles
from .model.codemodel import CodeModel, Target
from .model.codemodel import Directory as CodeModelDirectory
from .model.directory import Directory
from .model.index import Index

Expand Down Expand Up @@ -51,10 +52,14 @@ def make_class(self, data: InputDict, target: Type[T]) -> T:
Convert a dict to a dataclass. Automatically load a few nested jsonFile classes.
"""
if (
target in {CodeModel, Target, Cache, CMakeFiles, Directory}
target in {CodeModel, Target, Cache, CMakeFiles, CodeModelDirectory}
and "jsonFile" in data
and data["jsonFile"] is not None
):
# Transform CodeModelDirectory to Directory object
# TODO: inherit the fields from CodeModel counterparts
if target is CodeModelDirectory:
target = Directory # type: ignore[assignment]
return self._load_from_json(Path(data["jsonFile"]), target)

input_dict: Dict[str, Type[Any]] = {}
Expand Down
Loading