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):

  1. Query PyPI for the latest stable pre-commit release.

  2. Compare against the current lower bound in pyproject.toml.

  3. If a newer version exists, update the >= specifier.

Post-update validation (overridden here):

  1. Force recreation of the dev hatch environment to ensure fresh dependency resolution with the new pre-commit version.

  2. Run pre-commit run --all-files via the recreated dev environment.

  3. 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

PreCommitTool([project_root])

Manage pre-commit's version lower bound in pyproject.toml.

class mafw.devtools.toolchain.tools.precommit.PreCommitTool(project_root: Path | None = None)[source]

Bases: ProjectTool

Manage pre-commit’s version lower bound in pyproject.toml.

pre-commit is categorized as a project tool because its version is controlled through the dev optional-dependencies group in pyproject.toml. After an update, the dev hatch environment is recreated to ensure fresh dependency resolution, and then pre-commit run --all-files is 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. The post_update() is overridden to run pre-commit hooks, and verify() 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 dev hatch environment to ensure the newly specified pre-commit version is resolved and installed. Then runs pre-commit run --all-files via the dev environment to validate compatibility. If the command fails (non-zero exit code), pyproject.toml is reverted to its saved pre-update content and a DevtoolsError is raised.

Raises:

DevtoolsError – If environment recreation fails or if pre-commit run --all-files exits 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