mafw.devtools.toolchain.tools.precommit
Concrete ProjectTool implementation for pre-commit.
pre-commit is a project tool whose version lower bound is declared in
project.optional-dependencies.dev. This module manages the >=
specifier for pre-commit and validates updates by running
pre-commit run --all-files within the recreated dev hatch environment.
Update logic (inherited from ProjectTool):
Query PyPI for the latest stable pre-commit release.
Compare against the current lower bound in pyproject.toml.
If a newer version exists, update the
>=specifier.
Post-update validation (overridden here):
Force recreation of the
devhatch environment to ensure fresh dependency resolution with the new pre-commit version.Run
pre-commit run --all-filesvia the recreated dev environment.If the command fails, revert pyproject.toml to its previous content.
The revert pattern uses _revert()
which restores the raw pyproject.toml content saved before the update.
Classes
|
Manage pre-commit's version lower bound in pyproject.toml. |
- class mafw.devtools.toolchain.tools.precommit.PreCommitTool(project_root: Path | None = None)[source]
Bases:
ProjectToolManage pre-commit’s version lower bound in pyproject.toml.
pre-commit is categorized as a project tool because its version is controlled through the
devoptional-dependencies group inpyproject.toml. After an update, thedevhatch environment is recreated to ensure fresh dependency resolution, and thenpre-commit run --all-filesis executed to validate compatibility. If the command fails, pyproject.toml is reverted to its pre-update state.All standard behaviour (version detection, update, bootstrap) is provided by
ProjectTool. Thepost_update()is overridden to run pre-commit hooks, andverify()explicitly returns an empty list since pre-commit is referenced in a single location.- Parameters:
project_root (Path | None) – Path to the project root directory containing
pyproject.toml. Defaults to the current working directory.
- post_update() None[source]
Recreate the dev environment, then run pre-commit hooks; revert on failure.
Forces the recreation of the
devhatch environment to ensure the newly specified pre-commit version is resolved and installed. Then runspre-commit run --all-filesvia thedevenvironment to validate compatibility. If the command fails (non-zero exit code), pyproject.toml is reverted to its saved pre-update content and aDevtoolsErroris raised.- Raises:
DevtoolsError – If environment recreation fails or if
pre-commit run --all-filesexits with a non-zero code.
- verify() list[Issue][source]
Return an empty list — pre-commit is referenced in a single location.
pre-commit has no cross-file consistency to verify (unlike ruff which appears in both pyproject.toml and
.pre-commit-config.yaml).- Returns:
An empty list of issues.
- Return type:
list[toolchain.Issue]
- property env_name: str
Hatch environment where pre-commit hooks are run.
- Returns:
"dev"- Return type:
str
- property package_name: str
PyPI package name.
- Returns:
"pre-commit"- Return type:
str
- property section_path: str
TOML section path where pre-commit is declared.
- Returns:
"project.optional-dependencies.dev"- Return type:
str