diff --git a/docs/backend_authors.md b/docs/backend_authors.md index f80f0d8..e5cb0c6 100644 --- a/docs/backend_authors.md +++ b/docs/backend_authors.md @@ -88,10 +88,10 @@ that needs finer control. ## Reading the configuration Parse `pyproject.toml` and take `tool.dynamic-metadata` as an **ordered list of -tables**. Each table has a required `provider`; every other key is -plugin-specific and passed through verbatim as that plugin's `settings`. -{func}`~dynamic_metadata.loader.entries_from_pyproject` reads and validates it, -returning `[]` if absent: +tables**. Each table has a required `provider` and an optional `tool`; every +other key is plugin-specific and passed through verbatim as that plugin's +`settings`. {func}`~dynamic_metadata.loader.entries_from_pyproject` reads and +validates it, returning `[]` if absent: ```python from dynamic_metadata.loader import entries_from_pyproject @@ -103,6 +103,29 @@ entries = entries_from_pyproject(pyproject) A field is only eligible if it appears in `project["dynamic"]`; the loader enforces this for you. +### Backend options in an entry + +The `tool` key in an entry is reserved for build backends (added in 0.7). Like +the top-level `[tool]` table, it holds one table per backend, named for that +backend, and each backend defines the contents of its own table. For example, a +backend can let a config-setting change a plugin setting: + +```toml +[[tool.dynamic-metadata]] +provider = "my_package.torch_deps" +variant = "cpu" +tool.scikit-build.config-settings.variant = "torch.variant" +``` + +A backend reads only its own table and ignores the tables of other backends, so +the static values in the entry stay valid for every backend. A backend that +changes settings from its table does so before it calls the loader. The loader +does not check the contents of `tool`. + +You MUST NOT pass `tool` to a provider hook. The loader removes it together with +`provider`. A loader from before 0.7 passes it through to the plugin as a +setting, and a plugin that rejects unknown settings then fails. + ## Collecting build requirements In your `get_requires_for_build_*` hooks, add anything the providers ask for. diff --git a/docs/backend_authors_reimplement.md b/docs/backend_authors_reimplement.md index 6f6f073..24fdddb 100644 --- a/docs/backend_authors_reimplement.md +++ b/docs/backend_authors_reimplement.md @@ -66,6 +66,7 @@ def load_dynamic_metadata(entries): for entry in entries: entry = dict(entry) provider = entry.pop("provider") + entry.pop("tool", None) # reserved for backends yield load_provider(provider), entry # entry is now `settings` ``` diff --git a/docs/plugin_authors.md b/docs/plugin_authors.md index 256d47b..38b4a58 100644 --- a/docs/plugin_authors.md +++ b/docs/plugin_authors.md @@ -53,6 +53,10 @@ project as resolved so far; read another field's value with `project["version"]`. The backend calls this hook in the same directory as PEP 517's hooks. +`settings` never contains the reserved entry keys `provider` and `tool`, so do +not use these names for settings. A backend may change a setting value (for +example, from a config-setting) before your hook runs. + ### Relative paths Every hook runs with the project root (the directory holding `pyproject.toml`) diff --git a/docs/users.md b/docs/users.md index 24256be..7752778 100644 --- a/docs/users.md +++ b/docs/users.md @@ -15,6 +15,10 @@ provider = "dynamic_metadata.regex" # ... plugin settings ... ``` +Two keys are reserved: `provider`, and `tool`, which holds options for your +build backend, such as `tool.scikit-build.config-settings` (see your backend's +docs). Neither is passed to the plugin. + Run `dynamic-metadata providers` to see the names available in your environment. A plugin that lives inside your own project rather than an installed distribution is instead given as an [inline table](#providing-a-custom-plugin) diff --git a/src/dynamic_metadata/loader.py b/src/dynamic_metadata/loader.py index fd0cae3..19ff7cc 100644 --- a/src/dynamic_metadata/loader.py +++ b/src/dynamic_metadata/loader.py @@ -257,6 +257,14 @@ def _validate_entries(entries: object) -> list[Mapping[str, Any]]: return list(entries) +# Keys the backend consumes; never passed to a provider. +RESERVED_KEYS = frozenset({"provider", "tool"}) + + +def _settings(entry: Mapping[str, Any]) -> dict[str, Any]: + return {k: v for k, v in entry.items() if k not in RESERVED_KEYS} + + def entries_from_pyproject(pyproject: Mapping[str, Any]) -> list[dict[str, Any]]: """Return the validated ``[[tool.dynamic-metadata]]`` entries of a parsed ``pyproject.toml``. @@ -278,14 +286,13 @@ def load_dynamic_metadata( ) -> Generator[tuple[DynamicMetadataProtocol, dict[str, Any]], None, None]: """Load each entry's provider, yielding it with its plugin settings. - Entries are processed in order; ``provider`` is consumed here and the - remaining keys are returned as plugin settings. + Entries are processed in order; the reserved keys ``provider`` and + ``tool`` are removed and the remaining keys are returned verbatim as + plugin settings. A backend that uses its ``tool.`` table to change + settings does so in the entries before calling this. """ for entry in _validate_entries(entries): - # 'provider' is the only key the loader consumes; the rest are plugin - # settings, passed through verbatim to the provider. - settings = {k: v for k, v in entry.items() if k != "provider"} - yield load_provider(entry["provider"]), settings + yield load_provider(entry["provider"]), _settings(entry) def process_dynamic_metadata( @@ -375,7 +382,7 @@ def get_requires_for_dynamic_metadata( """ requires: list[str] = [] for entry in _validate_entries(entries): - settings = {k: v for k, v in entry.items() if k != "provider"} + settings = _settings(entry) try: provider = load_provider(entry["provider"]) except ProviderLoadError: diff --git a/src/dynamic_metadata/resources/toml_schema.json b/src/dynamic_metadata/resources/toml_schema.json index cf6b7a5..51e358d 100644 --- a/src/dynamic_metadata/resources/toml_schema.json +++ b/src/dynamic_metadata/resources/toml_schema.json @@ -27,6 +27,10 @@ } } ] + }, + "tool": { + "type": "object", + "description": "Per-entry backend options, one table per backend (like the top-level tool table). Never passed to the plugin." } } } diff --git a/tests/test_package.py b/tests/test_package.py index 9c6772b..3fefab3 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -605,6 +605,45 @@ def test_load_dynamic_metadata_requires_provider_key() -> None: list(dynamic_metadata.loader.load_dynamic_metadata([{"field": "version"}])) +def test_tool_not_passed_to_provider(tmp_path: Path) -> None: + _write_provider( + tmp_path, + "strict_prov", + "def _check(settings):\n" + " assert set(settings) == {'value'}, settings\n" + "def dynamic_metadata(settings, project):\n" + " _check(settings)\n" + " return {'version': settings['value']}\n" + "def get_requires_for_dynamic_metadata(settings):\n" + " _check(settings)\n" + " return []\n" + "def dynamic_wheel(settings):\n" + " _check(settings)\n" + " return {}\n", + ) + entries = [ + { + "provider": {"path": str(tmp_path), "module": "strict_prov"}, + "value": "1.0", + "tool": {"backend": {"config-settings": {"value": "my.version"}}}, + } + ] + result = dynamic_metadata.loader.process_dynamic_metadata( + {"dynamic": ["version"]}, entries, "wheel" + ) + assert result["version"] == "1.0" + assert dynamic_metadata.loader.get_requires_for_dynamic_metadata(entries) == [] + assert dynamic_metadata.loader.dynamic_wheel_fields(entries) == set() + + +@pytest.mark.parametrize("value", ["x", {"backend": 1}, {"provider": "x"}]) +def test_tool_shape_not_checked(value: object) -> None: + # Backends own the contents; the loader only drops the key. + entry = {"provider": "dynamic_metadata.static", "tool": value} + [(_, settings)] = dynamic_metadata.loader.load_dynamic_metadata([entry]) + assert settings == {} + + def test_pep808_extends_static_dependencies() -> None: # PEP 808: a field may be both static and dynamic; the provider only adds. pyproject = dynamic_metadata.loader.process_dynamic_metadata(