Skip to content

⚡ [Feature]: Implement pre-commit hooks for automated code quality checks #62

@PPeitsch

Description

@PPeitsch

Problem Description

The project currently relies on manual execution of code quality tools like black, isort, and mypy, or on checks performed late in the development cycle by the CI pipeline. This workflow has several drawbacks:

  • It introduces a risk of inconsistent code formatting and style across the codebase.
  • Developers might forget to run these checks locally, leading to unnecessary CI failures that could have been caught earlier.
  • It adds cognitive load, as contributors must manually ensure their code adheres to all project standards before pushing changes.

Proposed Solution

Integrate pre-commit, a framework for managing and maintaining multi-language pre-commit hooks. This will automate code formatting, import sorting, and static type checking, ensuring these checks are performed automatically before any code is committed.

By enforcing these standards locally and automatically, we can improve code quality, streamline the development workflow, and reduce CI pipeline noise.

Alternative Solutions

  1. Rely solely on CI: Continue with the current approach where the CI pipeline is the only enforcer of code quality. This is less efficient as it catches issues late and requires developers to push fixes for minor formatting or type errors.
  2. Use Git Hooks Manually: Developers could set up their own local Git hooks. This approach is not scalable, is difficult to enforce consistently across all contributors, and requires manual setup for each developer.

Given the drawbacks of the alternatives, integrating pre-commit is the most effective and standard solution for a collaborative project.

Additional Context

  • This feature will improve the development experience for all current and future contributors.
  • It helps enforce the coding style guidelines defined in pyproject.toml automatically.
  • A successful implementation will involve an initial, one-time run of the hooks across the entire codebase to establish a consistent baseline.

Implementation Details

  1. Add Dependency: Add pre-commit to the dev group in the project's pyproject.toml optional dependencies.
  2. Create Config File: Create a .pre-commit-config.yaml file in the project root.
  3. Configure Hooks: Configure the repositories and hooks for the following tools:
    • pre-commit-hooks: For basic checks like trailing-whitespace, end-of-file-fixer, check-yaml, and check-toml.
    • black: For code formatting.
    • isort: For import sorting.
    • mypy: For static type checking.
  4. Install Hooks: Instruct developers to run pre-commit install once to set up the git hook scripts in their local repository.
  5. Initial Run: Perform a one-time execution across all files with pre-commit run --all-files to apply formatting and fix issues project-wide. This will be part of the implementation PR.
  6. Update Documentation: Add a section to CONTRIBUTING.md detailing the pre-commit setup and explaining its purpose to new contributors.

Guidelines

  • I agree to follow this project's Contributing Guidelines
  • I have searched for similar feature requests
  • I understand that this is a request and implementation is not guaranteed

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions