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
- 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.
- 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
- Add Dependency: Add
pre-commit to the dev group in the project's pyproject.toml optional dependencies.
- Create Config File: Create a
.pre-commit-config.yaml file in the project root.
- 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.
- Install Hooks: Instruct developers to run
pre-commit install once to set up the git hook scripts in their local repository.
- 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.
- Update Documentation: Add a section to
CONTRIBUTING.md detailing the pre-commit setup and explaining its purpose to new contributors.
Guidelines
Problem Description
The project currently relies on manual execution of code quality tools like
black,isort, andmypy, or on checks performed late in the development cycle by the CI pipeline. This workflow has several drawbacks: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
Given the drawbacks of the alternatives, integrating
pre-commitis the most effective and standard solution for a collaborative project.Additional Context
pyproject.tomlautomatically.Implementation Details
pre-committo thedevgroup in the project'spyproject.tomloptional dependencies..pre-commit-config.yamlfile in the project root.pre-commit-hooks: For basic checks liketrailing-whitespace,end-of-file-fixer,check-yaml, andcheck-toml.black: For code formatting.isort: For import sorting.mypy: For static type checking.pre-commit installonce to set up the git hook scripts in their local repository.pre-commit run --all-filesto apply formatting and fix issues project-wide. This will be part of the implementation PR.CONTRIBUTING.mddetailing thepre-commitsetup and explaining its purpose to new contributors.Guidelines