๐ง Pre-Commit Config File
Maintaining code quality is essential in software development, and automating code checks can significantly improve the consistency and reliability of your codebase. pre-commit is a popular framework that allows you to define and enforce code quality checks before committing your code. By configuring a pre-commit-config.yaml file, you can define the hooks and checks to run automatically during the commit process.
Let's take a closer look at a sample pre-commit-config.yaml file and explore its various sections:
default_stages: [commit]
repos:
- repo: local
hooks:
- id: black
name: Black
entry: black
types: [python]
language: system
- id: system
name: Isort
entry: isort
types: [python]
language: system
- id: pycodestyle
name: Pycodestyle
entry: pycodestyle
args:
[
"--config=pyproject.toml",
]
types: [python]
language: system
- id: pylint
name: Pylint
entry: pylint
args:
[
"--rcfile=pyproject.toml",
]
types: [python]
language: system
- id: pydocstyle
name: pydocstyle
entry: pydocstyle
types: [python]
language: system
Default Stages
The default_stages field specifies the default stages in which the hooks defined in this configuration file will run. In this example, the hooks will run during the commit stage. You can modify this field to fit your specific workflow requirements.
The available stages are:
post-checkoutpost-mergepost-rewritepre-commitpre-merge-commitpre-pushpre-rebase
Repositories and Hooks
The repos section allows you to define the repositories and associated hooks. In this example, a local repository is defined with multiple hooks for various code quality checks.
-
The
blackhook enforces code formatting using the Black code formatter. It runs theblackcommand on Python files (types: [python]) using the system's default language. -
The
systemhook executes the Isort utility to sort imports in Python files. It uses theisortcommand and operates on Python files. -
The
pycodestylehook runs the Pycodestyle linter, also known as pep8, to check code style compliance. It uses thepycodestylecommand and specifies the--config=pyproject.tomlargument to provide a custom configuration file. -
The
pylinthook performs static code analysis using the Pylint linter. It runs thepylintcommand and passes the--rcfile=pyproject.tomlargument to use a custom configuration file. -
The
pydocstylehook checks adherence to docstring conventions using thepydocstylecommand. It operates on Python files.
Feel free to customize and add more hooks based on your specific code quality requirements.
By configuring the pre-commit-config.yaml file, you can automate code quality checks and enforce consistent coding standards in your project. The provided example showcases common hooks for code formatting, import sorting, code style, static analysis, and docstring conventions. However, you can adapt and extend the configuration based on your project's needs and preferences.
Remember, maintaining high code quality is an ongoing process, and leveraging tools like pre-commit can greatly enhance the efficiency and reliability of your development workflow. Happy coding!