Skip to content

Commit e2b935e

Browse files
committed
Move the special default handling to the dataclass definition
Signed-off-by: Cristian Le <[email protected]>
1 parent bce4a18 commit e2b935e

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/scikit_build_core/settings/documentation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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

1516
if TYPE_CHECKING:
@@ -26,6 +27,9 @@ def __dir__() -> list[str]:
2627
return __all__
2728

2829

30+
version_display = ".".join(__version__.split(".")[:2])
31+
32+
2933
def _get_value(value: ast.expr) -> str:
3034
assert isinstance(value, ast.Constant)
3135
assert isinstance(value.value, str)
@@ -93,7 +97,12 @@ def mk_docs(dc: type[object], prefix: str = "") -> Generator[DCDoc, None, None]:
9397
yield from mk_docs(field_type, prefix=f"{prefix}{field.name}[].")
9498
continue
9599

96-
if field.default is not dataclasses.MISSING and field.default is not None:
100+
if default_before_format := get_metadata_field(field, "display_default", None):
101+
assert isinstance(default_before_format, str)
102+
default = default_before_format.format(
103+
version=version_display,
104+
)
105+
elif field.default is not dataclasses.MISSING and field.default is not None:
97106
default = repr(
98107
str(field.default)
99108
if isinstance(field.default, (Path, Version, SpecifierSet))

src/scikit_build_core/settings/skbuild_docs.py

Lines changed: 3 additions & 13 deletions
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,22 +10,13 @@ def __dir__() -> list[str]:
1110
return __all__
1211

1312

14-
version = ".".join(__version__.split(".")[:2])
15-
16-
1713
def mk_skbuild_docs() -> str:
1814
"""
1915
Makes documentation for the skbuild model.
2016
"""
21-
items = [x for x in mk_docs(ScikitBuildSettings) if not x.deprecated]
22-
for item in items:
23-
if item.name == "minimum-version":
24-
item.default = f'"{version}" # current version'
25-
if item.name == "install.strip":
26-
item.default = "true"
27-
if item.name == "wheel.packages":
28-
item.default = '["src/<package>", "python/<package>", "<package>"]'
29-
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+
)
3020

3121

3222
if __name__ == "__main__":

src/scikit_build_core/settings/skbuild_model.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SettingsFieldMetadata(Mapping[str, Any]):
3636
Convenience dataclass to store field metadata for documentation.
3737
"""
3838

39+
display_default: Optional[str] = None
3940
deprecated: bool = False
4041

4142
def __getitem__(self, key: str) -> Any:
@@ -198,7 +199,12 @@ class SDistSettings:
198199

199200
@dataclasses.dataclass
200201
class WheelSettings:
201-
packages: Optional[Union[List[str], Dict[str, str]]] = None
202+
packages: Optional[Union[List[str], Dict[str, str]]] = dataclasses.field(
203+
default=None,
204+
metadata=SettingsFieldMetadata(
205+
display_default='["src/<package>", "python/<package>", "<package>"]'
206+
),
207+
)
202208
"""
203209
A list of packages to auto-copy into the wheel. If this is not set, it will
204210
default to the first of ``src/<package>``, ``python/<package>``, or
@@ -324,7 +330,9 @@ class InstallSettings:
324330
The components to install. If empty, all default components are installed.
325331
"""
326332

327-
strip: Optional[bool] = None
333+
strip: Optional[bool] = dataclasses.field(
334+
default=None, metadata=SettingsFieldMetadata(display_default="true")
335+
)
328336
"""
329337
Whether to strip the binaries. True for release builds on scikit-build-core
330338
0.5+ (0.5-0.10.5 also incorrectly set this for debug builds).
@@ -406,7 +414,12 @@ class ScikitBuildSettings:
406414
Enable early previews of features not finalized yet.
407415
"""
408416

409-
minimum_version: Optional[Version] = None
417+
minimum_version: Optional[Version] = dataclasses.field(
418+
default=None,
419+
metadata=SettingsFieldMetadata(
420+
display_default='"{version}" # current version'
421+
),
422+
)
410423
"""
411424
If set, this will provide a method for backward compatibility.
412425
"""

0 commit comments

Comments
 (0)