From 2fd7e73d95a8a8406be6295a603b113ffa42c9ef Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Fri, 24 Jul 2026 18:57:15 -0700 Subject: [PATCH 1/2] configure hook method --- src/pdm/backend/base.py | 9 ++++++++- src/pdm/backend/config.py | 10 ++++++---- src/pdm/backend/hooks/base.py | 6 ++++++ tests/fixtures/hooks/hook_class.py | 3 +++ tests/fixtures/hooks/hook_module.py | 4 ++++ tests/fixtures/hooks/local_hook.py | 4 ++++ tests/test_hooks.py | 1 + 7 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/pdm/backend/base.py b/src/pdm/backend/base.py index 7ae9972..9c86faf 100644 --- a/src/pdm/backend/base.py +++ b/src/pdm/backend/base.py @@ -99,7 +99,7 @@ def __init__( ) -> None: self._old_cwd: str | None = None self.location = Path(location) - self.config = Config.from_pyproject(self.location) + self.config = Config.from_pyproject(self.location, validate=False) self.config_settings = dict(config_settings or {}) self._hooks = list(self.get_hooks()) @@ -160,6 +160,11 @@ def __exit__(self, *args: Any) -> None: assert self._old_cwd os.chdir(self._old_cwd) + def configure(self, context: Context) -> None: + """Finalize and validate configuration, calling hooks""" + self.call_hook("pdm_configure", context) + self.config.validate() + def clean(self, context: Context) -> None: """Clean up the build directory.""" self.call_hook("pdm_build_clean", context) @@ -202,6 +207,8 @@ def finalize(self, context: Context, artifact: Path) -> None: def build(self, build_dir: str, **kwargs: Any) -> Path: """Build the package and return the path to the artifact.""" context = self.build_context(Path(build_dir), **kwargs) + self.configure(context) + should_clean = True if "no-clean-build" in self.config_settings: diff --git a/src/pdm/backend/config.py b/src/pdm/backend/config.py index 382e5f4..d3a638d 100644 --- a/src/pdm/backend/config.py +++ b/src/pdm/backend/config.py @@ -35,16 +35,18 @@ class Config: Parameters: root: The root directory of the project data: The parsed pyproject.toml data + validate: Perform validation immediately on instantiation. Attributes: metadata (Metadata): The project metadata from the `project` table build_config (BuildConfig): The build config from the `tool.pdm.build` table """ - def __init__(self, root: Path, data: dict[str, Any]) -> None: + def __init__(self, root: Path, data: dict[str, Any], validate: bool = True) -> None: self.root = root self.data = data - self.validate() + if validate: + self.validate() def validate(self) -> StandardMetadata: """Validate the pyproject.toml data.""" @@ -64,7 +66,7 @@ def build_config(self) -> BuildConfig: ) @classmethod - def from_pyproject(cls, root: str | Path) -> Config: + def from_pyproject(cls, root: str | Path, validate: bool = True) -> Config: """Load the pyproject.toml file from the given project root.""" root = Path(root) pyproject = root / "pyproject.toml" @@ -75,7 +77,7 @@ def from_pyproject(cls, root: str | Path) -> Config: data = tomllib.load(fp) except tomllib.TOMLDecodeError as e: raise ConfigError(f"Invalid pyproject.toml file: {e}") from e - return cls(root, data) + return cls(root, data, validate=validate) def write_to(self, path: str | Path) -> None: """Write the pyproject.toml file to the given path.""" diff --git a/src/pdm/backend/hooks/base.py b/src/pdm/backend/hooks/base.py index 8b7b334..c9d8b94 100644 --- a/src/pdm/backend/hooks/base.py +++ b/src/pdm/backend/hooks/base.py @@ -88,6 +88,12 @@ def pdm_build_hook_enabled(self, context: Context) -> bool: """ ... + def pdm_build_configure(self, context: Context) -> None: + """Wraps the pyproject.toml configuration loading and validation. + Modify values or prepare the build environment before the config is validated. + """ + ... + def pdm_build_clean(self, context: Context) -> None: """An optional clean step which will be called before the build starts diff --git a/tests/fixtures/hooks/hook_class.py b/tests/fixtures/hooks/hook_class.py index b845a53..86b74e1 100644 --- a/tests/fixtures/hooks/hook_class.py +++ b/tests/fixtures/hooks/hook_class.py @@ -13,6 +13,9 @@ class BuildHook: def __init__(self, name: str = "2") -> None: self.name = name + def pdm_configure(self, context: Context) -> None: + logger.info("Hook%s configure called", self.name) + def pdm_build_clean(self, context: Context) -> None: logger.info("Hook%s build clean called", self.name) diff --git a/tests/fixtures/hooks/hook_module.py b/tests/fixtures/hooks/hook_module.py index 31494dc..4530d91 100644 --- a/tests/fixtures/hooks/hook_module.py +++ b/tests/fixtures/hooks/hook_module.py @@ -8,6 +8,10 @@ logger = logging.getLogger("hooks") +def pdm_configure(context: Context) -> None: + logger.info("Hook1 configure called") + + def pdm_build_clean(context: Context) -> None: logger.info("Hook1 build clean called") diff --git a/tests/fixtures/hooks/local_hook.py b/tests/fixtures/hooks/local_hook.py index 891066e..7bead73 100644 --- a/tests/fixtures/hooks/local_hook.py +++ b/tests/fixtures/hooks/local_hook.py @@ -8,6 +8,10 @@ logger = logging.getLogger("hooks") +def pdm_configure(context: Context) -> None: + logger.info("Hook4 configure called") + + def pdm_build_clean(context: Context) -> None: logger.info("Hook4 build clean called") diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 40c15ec..2b047fa 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -43,6 +43,7 @@ def test_load_hooks(project_with_hooks, caplog: pytest.LogCaptureFixture): messages = [record.message for record in caplog.records if record.name == "hooks"] for num in range(1, 5): + assert f"Hook{num} configure called" in messages assert f"Hook{num} build clean called" in messages assert f"Hook{num} build initialize called" in messages assert f"Hook{num} build update files called" in messages From bdf7688f336a67cde09b22acae28e93c87b4e754 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Fri, 24 Jul 2026 19:06:58 -0700 Subject: [PATCH 2/2] make name consistent with other hooks --- src/pdm/backend/base.py | 2 +- tests/fixtures/hooks/hook_class.py | 4 ++-- tests/fixtures/hooks/hook_module.py | 4 ++-- tests/fixtures/hooks/local_hook.py | 4 ++-- tests/test_hooks.py | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pdm/backend/base.py b/src/pdm/backend/base.py index 9c86faf..bfda310 100644 --- a/src/pdm/backend/base.py +++ b/src/pdm/backend/base.py @@ -162,7 +162,7 @@ def __exit__(self, *args: Any) -> None: def configure(self, context: Context) -> None: """Finalize and validate configuration, calling hooks""" - self.call_hook("pdm_configure", context) + self.call_hook("pdm_build_configure", context) self.config.validate() def clean(self, context: Context) -> None: diff --git a/tests/fixtures/hooks/hook_class.py b/tests/fixtures/hooks/hook_class.py index 86b74e1..b53b639 100644 --- a/tests/fixtures/hooks/hook_class.py +++ b/tests/fixtures/hooks/hook_class.py @@ -13,8 +13,8 @@ class BuildHook: def __init__(self, name: str = "2") -> None: self.name = name - def pdm_configure(self, context: Context) -> None: - logger.info("Hook%s configure called", self.name) + def pdm_build_configure(self, context: Context) -> None: + logger.info("Hook%s build configure called", self.name) def pdm_build_clean(self, context: Context) -> None: logger.info("Hook%s build clean called", self.name) diff --git a/tests/fixtures/hooks/hook_module.py b/tests/fixtures/hooks/hook_module.py index 4530d91..a2c3f17 100644 --- a/tests/fixtures/hooks/hook_module.py +++ b/tests/fixtures/hooks/hook_module.py @@ -8,8 +8,8 @@ logger = logging.getLogger("hooks") -def pdm_configure(context: Context) -> None: - logger.info("Hook1 configure called") +def pdm_build_configure(context: Context) -> None: + logger.info("Hook1 build configure called") def pdm_build_clean(context: Context) -> None: diff --git a/tests/fixtures/hooks/local_hook.py b/tests/fixtures/hooks/local_hook.py index 7bead73..458b443 100644 --- a/tests/fixtures/hooks/local_hook.py +++ b/tests/fixtures/hooks/local_hook.py @@ -8,8 +8,8 @@ logger = logging.getLogger("hooks") -def pdm_configure(context: Context) -> None: - logger.info("Hook4 configure called") +def pdm_build_configure(context: Context) -> None: + logger.info("Hook4 build configure called") def pdm_build_clean(context: Context) -> None: diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 2b047fa..7da9f2b 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -43,7 +43,7 @@ def test_load_hooks(project_with_hooks, caplog: pytest.LogCaptureFixture): messages = [record.message for record in caplog.records if record.name == "hooks"] for num in range(1, 5): - assert f"Hook{num} configure called" in messages + assert f"Hook{num} build configure called" in messages assert f"Hook{num} build clean called" in messages assert f"Hook{num} build initialize called" in messages assert f"Hook{num} build update files called" in messages