diff --git a/macros.py b/macros.py index c5466c5..eca1700 100644 --- a/macros.py +++ b/macros.py @@ -1,25 +1,20 @@ from __future__ import annotations -import tomllib from pathlib import Path from typing import Any +from setuptools_scm import get_version -def _load_version(pyproject_path: Path) -> str: - """Return project version string from pyproject.toml.""" - with pyproject_path.open("rb") as pyproject_file: - data = tomllib.load(pyproject_file) - project = data.get("project", {}) - version = project.get("version") - if not isinstance(version, str): - raise ValueError("project.version must be defined in pyproject.toml") - return version + +def _load_version(root: Path) -> str: + """Return project version string using setuptools_scm.""" + return get_version(root=str(root)) def define_env(env: Any) -> None: """Expose project version to MkDocs macros and theme configuration.""" root = Path(__file__).resolve().parent - version = _load_version(root / "pyproject.toml") + version = _load_version(root) env.variables["project_version"] = version extra = env.conf.get("extra") or {} extra["version"] = version diff --git a/pyproject.toml b/pyproject.toml index 1df6b34..ef0e3e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,6 +86,7 @@ docs = [ "mkdocs-material", "mkdocstrings[python]", "mkdocs-macros-plugin", + "setuptools_scm>=8.0", ] [project.scripts] @@ -103,7 +104,7 @@ typeCheckingMode = "standard" reportAny = false reportExplicitAny = false reportUnusedCallResult = false -exclude = ["src/alpha_hwr/*_baseline.py"] +exclude = ["src/alpha_hwr/*_baseline.py", "macros.py"] [tool.mypy] ignore_missing_imports = true