https://github.com/klieret/python-pre-commit-demo-tutorial
Try out useful pre-commits for python
https://github.com/klieret/python-pre-commit-demo-tutorial
pre-commit teaching-materials training-materials
Last synced: 10 months ago
JSON representation
Try out useful pre-commits for python
- Host: GitHub
- URL: https://github.com/klieret/python-pre-commit-demo-tutorial
- Owner: klieret
- License: mit
- Created: 2022-08-03T16:08:30.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-12-03T00:42:41.000Z (over 1 year ago)
- Last Synced: 2024-12-03T01:29:51.618Z (over 1 year ago)
- Topics: pre-commit, teaching-materials, training-materials
- Language: Python
- Homepage: https://klieret.github.io/everything-you-didnt-now-you-needed/
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Exercises: pre-commit for python projects
[](https://git-scm.com/book/en/v2/GitHub-Contributing-to-a-Project)
This repository demonstrates how you can catch a lot of python issues with
[pre-commit](https://pre-commit.com/).
Part of the lecture [Everything you didn't know you needed](https://github.com/klieret/everything-you-didnt-now-you-needed).
## 📦 Exercise setup
Clone the demo repository
```bash
git clone https://github.com/klieret/python-pre-commit-demo-tutorial.git
cd python-pre-commit-demo-tutorial
```
If you haven't done already, install the `pre-commit` package:
```bash
pip3 install pipx
pipx install pre-commit
```
Let's take a look at the `pre-commit` configuration from the repository:
```bash
cat .pre-commit-config.yaml
```
Let's install these hooks:
```bash
pre-commit install
```
## 🔥 Exercises
Now let's open the `test.py` file in your favorite editor.
Try one/each of the following modifications to `test.py`.
After every modification, try to commit your change (`git commit -a`) and
see what happens.
Alternatively, you can also simply run `pre-commit run -a` to run `pre-commit`
over all files.
1. Replace `return a + b` with `return None` (`mypy` will complain!)
2. Remove spaces between `+` (`black` will automatically reformat the file and add the spaces back)
3. Change `the` to `teh` in the docstring (`codespell` will flag it as a spelling mistake)
4. Import `math` (`flake8` will complain about an unused iport)
5. Add additional whitespace at the end of any line (`end-of-line-fixer` will fix it for you)
> **Note**:
> If you ever get stuck, run `git stash` ("stashes" away your changes) or
> `git reset --hard` (don't do that on an important repository without understanding
> the implications).