Repository: scikit-build/scikit-build-core
Pull request #699 (2024-04-12 03:27:13)

refactor: GetRequires args changed
This is needed for https://github.com/scikit-build/scikit-build-core/pull/637#issuecomment-2050665306.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build) Report
Attention: Patch coverage is `73.33333%` with `4 lines` in your changes are missing coverage. Please review.
> Project coverage is 86.23%. Comparing base [(`5aea532`)](https://app.codecov.io/gh/scikit-build/scikit-build-core/commit/5aea532f7d4fe4de56696096543c14d3d9ca208b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build) to head [(`7e15390`)](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build).

| [Files](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build) | Patch % | Lines |
|---|---|---|
| [src/scikit\_build\_core/setuptools/build\_meta.py](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?src=pr&el=tree&filepath=src%2Fscikit_build_core%2Fsetuptools%2Fbuild_meta.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build#diff-c3JjL3NjaWtpdF9idWlsZF9jb3JlL3NldHVwdG9vbHMvYnVpbGRfbWV0YS5weQ==) | 0.00% | [3 Missing :warning: ](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build) |
| [src/scikit\_build\_core/builder/get\_requires.py](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?src=pr&el=tree&filepath=src%2Fscikit_build_core%2Fbuilder%2Fget_requires.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build#diff-c3JjL3NjaWtpdF9idWlsZF9jb3JlL2J1aWxkZXIvZ2V0X3JlcXVpcmVzLnB5) | 88.88% | [1 Missing :warning: ](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build) |

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #699   +/-   ##
=======================================
  Coverage   86.23%   86.23%           
=======================================
  Files          64       64           
  Lines        3321     3321           
=======================================
  Hits         2864     2864           
  Misses        457      457           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/scikit-build/scikit-build-core/pull/699?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scikit-build).


Patch:
diff --git a/src/scikit_build_core/build/__init__.py b/src/scikit_build_core/build/__init__.py
index 9192479..941b8d6 100644
--- a/src/scikit_build_core/build/__init__.py
+++ b/src/scikit_build_core/build/__init__.py
@@ -101,7 +101,7 @@ def get_requires_for_build_sdist(
 ) -> list[str]:
     from ..builder.get_requires import GetRequires
 
-    requires = GetRequires(config_settings)
+    requires = GetRequires.from_config_settings(config_settings)
 
     # These are only injected if cmake is required for the SDist step
     cmake_requires = (
@@ -121,7 +121,7 @@ def get_requires_for_build_wheel(
 ) -> list[str]:
     from ..builder.get_requires import GetRequires
 
-    requires = GetRequires(config_settings)
+    requires = GetRequires.from_config_settings(config_settings)
 
     # These are only injected if cmake is required for the wheel step
     cmake_requires = (
@@ -141,7 +141,7 @@ def get_requires_for_build_editable(
 ) -> list[str]:
     from ..builder.get_requires import GetRequires
 
-    requires = GetRequires(config_settings)
+    requires = GetRequires.from_config_settings(config_settings)
 
     # These are only injected if cmake is required for the wheel step
     cmake_requires = (
diff --git a/src/scikit_build_core/builder/get_requires.py b/src/scikit_build_core/builder/get_requires.py
index 0f7d751..686048d 100644
--- a/src/scikit_build_core/builder/get_requires.py
+++ b/src/scikit_build_core/builder/get_requires.py
@@ -24,7 +24,7 @@ from ..settings.skbuild_read_settings import SettingsReader
 if TYPE_CHECKING:
     from collections.abc import Generator, Mapping
 
-    from .._compat.typing import Literal
+    from .._compat.typing import Literal, Self
     from ..settings.skbuild_model import ScikitBuildSettings
 
 __all__ = ["GetRequires"]
@@ -45,18 +45,23 @@ def is_known_platform(platforms: frozenset[str]) -> bool:
     return any(tag.platform in platforms for tag in sys_tags())
 
 
-@dataclasses.dataclass
-class GetRequires:
-    config_settings: Mapping[str, list[str] | str] | None = None
+def _load_scikit_build_settings(
+    config_settings: Mapping[str, list[str] | str] | None = None,
+) -> ScikitBuildSettings:
+    return SettingsReader.from_file("pyproject.toml", config_settings).settings
 
-    def __post_init__(self) -> None:
-        self._settings = SettingsReader.from_file(
-            "pyproject.toml", self.config_settings
-        ).settings
 
-    @property
-    def settings(self) -> ScikitBuildSettings:
-        return self._settings
+@dataclasses.dataclass(frozen=True)
+class GetRequires:
+    settings: ScikitBuildSettings = dataclasses.field(
+        default_factory=_load_scikit_build_settings
+    )
+
+    @classmethod
+    def from_config_settings(
+        cls, config_settings: Mapping[str, list[str] | str] | None
+    ) -> Self:
+        return cls(_load_scikit_build_settings(config_settings))
 
     def cmake(self) -> Generator[str, None, None]:
         if os.environ.get("CMAKE_EXECUTABLE", ""):
diff --git a/src/scikit_build_core/setuptools/build_meta.py b/src/scikit_build_core/setuptools/build_meta.py
index 569367a..d426528 100644
--- a/src/scikit_build_core/setuptools/build_meta.py
+++ b/src/scikit_build_core/setuptools/build_meta.py
@@ -42,7 +42,7 @@ def get_requires_for_build_sdist(
     setuptools_reqs = setuptools.build_meta.get_requires_for_build_sdist(
         config_settings
     )
-    requires = GetRequires(config_settings)
+    requires = GetRequires.from_config_settings(config_settings)
 
     # These are only injected if cmake is required for the SDist step
     cmake_requires = (
@@ -54,7 +54,7 @@ def get_requires_for_build_sdist(
 def get_requires_for_build_wheel(
     config_settings: dict[str, str | list[str]] | None = None,
 ) -> list[str]:
-    requires = GetRequires(config_settings)
+    requires = GetRequires.from_config_settings(config_settings)
 
     setuptools_reqs = setuptools.build_meta.get_requires_for_build_wheel(
         config_settings
@@ -68,7 +68,7 @@ if hasattr(setuptools.build_meta, "get_requires_for_build_editable"):
     def get_requires_for_build_editable(
         config_settings: dict[str, str | list[str]] | None = None,
     ) -> list[str]:
-        requires = GetRequires(config_settings)
+        requires = GetRequires.from_config_settings(config_settings)
         setuptools_reqs = setuptools.build_meta.get_requires_for_build_editable(
             config_settings
         )


Test patch:
diff --git a/tests/test_get_requires.py b/tests/test_get_requires.py
index d673c33..6a060c0 100644
--- a/tests/test_get_requires.py
+++ b/tests/test_get_requires.py
@@ -71,8 +71,8 @@ def test_get_requires_parts_settings(fp):
         stdout='{"version":{"string":"3.18.0"}}',
     )
     config = {"cmake.version": ">=3.20"}
-    assert set(GetRequires(config).cmake()) == {"cmake>=3.20"}
-    assert set(GetRequires(config).ninja()) == {*ninja}
+    assert set(GetRequires.from_config_settings(config).cmake()) == {"cmake>=3.20"}
+    assert set(GetRequires.from_config_settings(config).ninja()) == {*ninja}
 
 
 def test_get_requires_parts_pyproject(fp, monkeypatch, tmp_path):