Example repository for article on setting up pre-commit hooks.
Python >= 3.8
NOTE: If you choose to use GitHub Codespaces with this repository, you can skip ahead to step 4.
-
Clone this repository and change to the directory for this repository:
git clone git@github.com:stefmolin/pre-commit-example.git && \ cd pre-commit-example;
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
# bash/zsh source venv/bin/activate # Windows cmd.exe .\venv\Scripts\activate.bat
-
Install the package (update
pip
if you encounter errors):python -m pip install -e .
-
Install the pre-commit hooks in this repository:
pre-commit install
-
Create the file
src/example/utils.py
as follows and save it:import re def my_function(a): """My function.""" pass
-
Try to commit this file:
git add src/example/utils.py git commit -m "Add utils.py"
The pre-commit hooks will run, and your commit will be stopped since there are some violations:
-
Fix those issues (example below) and then try to commit again.
"""Utility functions.""" def my_function(a): """ My function. Parameters ---------- a : int The value to use. """ pass
If you fixed everything, your commit will be successful:
For an explanation of the setup, please consult the accompanying article.
Stefanie Molin (@stefmolin) is a software engineer and data scientist at Bloomberg in New York City, where she tackles tough problems in information security, particularly those revolving around data wrangling/visualization, building tools for gathering data, and knowledge sharing. She is also the author of Hands-On Data Analysis with Pandas, which is currently in its second edition and has been translated into Korean. She holds a bachelor’s of science degree in operations research from Columbia University's Fu Foundation School of Engineering and Applied Science, as well as a master’s degree in computer science, with a specialization in machine learning, from Georgia Tech. In her free time, she enjoys traveling the world, inventing new recipes, and learning new languages spoken among both people and computers.