Skip to content

Commit 5a8a8f4

Browse files
LecrisUThenryiii
andauthored
doc: add a field metadata to the setting fields (#1051)
A few preparations for #999 --------- Signed-off-by: Cristian Le <[email protected]> Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent d123fd2 commit 5a8a8f4

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

src/scikit_build_core/settings/documentation.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
import dataclasses
55
import inspect
66
import textwrap
7+
import typing
78
from pathlib import Path
8-
from typing import TYPE_CHECKING
99

1010
from packaging.specifiers import SpecifierSet
1111
from packaging.version import Version
1212

13+
from .. import __version__
1314
from .._compat.typing import get_args, get_origin
1415

15-
if TYPE_CHECKING:
16+
if typing.TYPE_CHECKING:
1617
from collections.abc import Generator
1718

19+
1820
__all__ = ["pull_docs"]
1921

2022

2123
def __dir__() -> list[str]:
2224
return __all__
2325

2426

27+
version_display = ".".join(__version__.split(".")[:2])
28+
29+
2530
def _get_value(value: ast.expr) -> str:
2631
assert isinstance(value, ast.Constant)
2732
assert isinstance(value.value, str)
@@ -45,17 +50,22 @@ def pull_docs(dc: type[object]) -> dict[str, str]:
4550
}
4651

4752

48-
@dataclasses.dataclass
53+
@dataclasses.dataclass(frozen=True)
4954
class DCDoc:
5055
name: str
5156
default: str
5257
docs: str
58+
deprecated: bool = False
5359

5460
def __str__(self) -> str:
5561
docs = "\n".join(f"# {s}" for s in textwrap.wrap(self.docs, width=78))
5662
return f"{docs}\n{self.name} = {self.default}\n"
5763

5864

65+
def sanitize_default_field(text: str) -> str:
66+
return text.replace("'", '"').replace("True", "true").replace("False", "false")
67+
68+
5969
def mk_docs(dc: type[object], prefix: str = "") -> Generator[DCDoc, None, None]:
6070
"""
6171
Makes documentation for a dataclass.
@@ -75,7 +85,12 @@ def mk_docs(dc: type[object], prefix: str = "") -> Generator[DCDoc, None, None]:
7585
yield from mk_docs(field_type, prefix=f"{prefix}{field.name}[].")
7686
continue
7787

78-
if field.default is not dataclasses.MISSING and field.default is not None:
88+
if default_before_format := field.metadata.get("display_default", None):
89+
assert isinstance(default_before_format, str)
90+
default = default_before_format.format(
91+
version=version_display,
92+
)
93+
elif field.default is not dataclasses.MISSING and field.default is not None:
7994
default = repr(
8095
str(field.default)
8196
if isinstance(field.default, (Path, Version, SpecifierSet))
@@ -87,7 +102,8 @@ def mk_docs(dc: type[object], prefix: str = "") -> Generator[DCDoc, None, None]:
87102
default = '""'
88103

89104
yield DCDoc(
90-
f"{prefix}{field.name}".replace("_", "-"),
91-
default.replace("'", '"').replace("True", "true").replace("False", "false"),
92-
docs[field.name],
105+
name=f"{prefix}{field.name}".replace("_", "-"),
106+
default=sanitize_default_field(default),
107+
docs=docs[field.name],
108+
deprecated=field.metadata.get("deprecated", False),
93109
)

src/scikit_build_core/settings/skbuild_docs.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from .. import __version__
43
from .documentation import mk_docs
54
from .skbuild_model import ScikitBuildSettings
65

@@ -11,24 +10,13 @@ def __dir__() -> list[str]:
1110
return __all__
1211

1312

14-
version = ".".join(__version__.split(".")[:2])
15-
16-
INV = {"cmake.minimum-version", "ninja.minimum-version"}
17-
18-
1913
def mk_skbuild_docs() -> str:
2014
"""
2115
Makes documentation for the skbuild model.
2216
"""
23-
items = [x for x in mk_docs(ScikitBuildSettings) if x.name not in INV]
24-
for item in items:
25-
if item.name == "minimum-version":
26-
item.default = f'"{version}" # current version'
27-
if item.name == "install.strip":
28-
item.default = "true"
29-
if item.name == "wheel.packages":
30-
item.default = '["src/<package>", "python/<package>", "<package>"]'
31-
return "\n".join(str(item) for item in items)
17+
return "\n".join(
18+
str(item) for item in mk_docs(ScikitBuildSettings) if not item.deprecated
19+
)
3220

3321

3422
if __name__ == "__main__":

src/scikit_build_core/settings/skbuild_model.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22
from pathlib import Path
3-
from typing import Any, Dict, List, Literal, Optional, Union
3+
from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
44

55
from packaging.specifiers import SpecifierSet
66
from packaging.version import Version
@@ -21,6 +21,7 @@
2121
"SDistSettings",
2222
"ScikitBuildSettings",
2323
"SearchSettings",
24+
"SettingsFieldMetadata",
2425
"WheelSettings",
2526
]
2627

@@ -29,6 +30,11 @@ def __dir__() -> List[str]:
2930
return __all__
3031

3132

33+
class SettingsFieldMetadata(TypedDict, total=False):
34+
display_default: Optional[str]
35+
deprecated: bool
36+
37+
3238
class CMakeSettingsDefine(str):
3339
"""
3440
A str subtype for automatically normalizing bool and list values
@@ -53,7 +59,9 @@ def escape_semicolons(item: str) -> str:
5359

5460
@dataclasses.dataclass
5561
class CMakeSettings:
56-
minimum_version: Optional[Version] = None
62+
minimum_version: Optional[Version] = dataclasses.field(
63+
default=None, metadata=SettingsFieldMetadata(deprecated=True)
64+
)
5765
"""
5866
DEPRECATED in 0.8; use version instead.
5967
"""
@@ -114,7 +122,9 @@ class SearchSettings:
114122

115123
@dataclasses.dataclass
116124
class NinjaSettings:
117-
minimum_version: Optional[Version] = None
125+
minimum_version: Optional[Version] = dataclasses.field(
126+
default=None, metadata=SettingsFieldMetadata(deprecated=True)
127+
)
118128
"""
119129
DEPRECATED in 0.8; use version instead.
120130
"""
@@ -174,7 +184,12 @@ class SDistSettings:
174184

175185
@dataclasses.dataclass
176186
class WheelSettings:
177-
packages: Optional[Union[List[str], Dict[str, str]]] = None
187+
packages: Optional[Union[List[str], Dict[str, str]]] = dataclasses.field(
188+
default=None,
189+
metadata=SettingsFieldMetadata(
190+
display_default='["src/<package>", "python/<package>", "<package>"]'
191+
),
192+
)
178193
"""
179194
A list of packages to auto-copy into the wheel. If this is not set, it will
180195
default to the first of ``src/<package>``, ``python/<package>``, or
@@ -300,7 +315,9 @@ class InstallSettings:
300315
The components to install. If empty, all default components are installed.
301316
"""
302317

303-
strip: Optional[bool] = None
318+
strip: Optional[bool] = dataclasses.field(
319+
default=None, metadata=SettingsFieldMetadata(display_default="true")
320+
)
304321
"""
305322
Whether to strip the binaries. True for release builds on scikit-build-core
306323
0.5+ (0.5-0.10.5 also incorrectly set this for debug builds).
@@ -382,7 +399,12 @@ class ScikitBuildSettings:
382399
Enable early previews of features not finalized yet.
383400
"""
384401

385-
minimum_version: Optional[Version] = None
402+
minimum_version: Optional[Version] = dataclasses.field(
403+
default=None,
404+
metadata=SettingsFieldMetadata(
405+
display_default='"{version}" # current version'
406+
),
407+
)
386408
"""
387409
If set, this will provide a method for backward compatibility.
388410
"""

0 commit comments

Comments
 (0)