On the last day of January I made my first Pull Request, but take a few steps.
What's Pre-commit
Isn't it great to be able to run a script before every git commit? Here come Git Hooks!
git commit
It is a good choice to set up some configuration about Git Hook, it makes the code more consistent.
How it works
After a quick install
pip install pre-commit
you can configure every hooks that you need. The purpuse of my Pull Request was to add Ruff which we'll discuss later.
Create your pre-commit-config.yaml
, mine was something like that:
ci:
autofix_commit_msg: |
ci: auto fixes from pre-commit hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_commit_msg: 'ci: pre-commit autoupdate'
autoupdate_schedule: monthly
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: ["--py38-plus"]
- repo: https://github.com/adamchainz/django-upgrade
rev: '1.15.0'
hooks:
- id: django-upgrade
args: [--target-version, "3.2"]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
As you can see there's a section with CI that runs pre-commit even when you make a pull request
ci:
autofix_commit_msg: |
ci: auto fixes from pre-commit hooks
for more information, see https://pre-commit.ci
autofix_prs: true
autoupdate_commit_msg: 'ci: pre-commit autoupdate'
autoupdate_schedule: monthly
Ruff: linting and formatting
Ruff is able to both lint and format your code.
Install Ruff on your machine.
pip install ruff
Linting is extreamly useful when you need to get rid of a bunch of 'grammatical' code errors, here's a list of what you can sometimes get rid of by simply using --fixing
, like magic (check the ๐ ๏ธ for automatic fixes )
ruff check . --fix
How can I do magic by myself ?
Easy! just in few steps:
Install Ruff
Run
ruff check .
Run
ruff check . --fix
Admire your magic
On your project (How it really works)
Sometimes magic needs a little help from the configuration. You need to specify the rules to check in Ruff's configuration. Here is mine inside pyproject.toml
:
[tool.ruff.lint]
select=[
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
"PLE", # pylint error
"PLR", # pylint refactor
"PLW", # pylint warning
"UP", # pyupgrade
]
extend-ignore = [
"F841" # unused variable rules
]
Setting Rules for Ruff Checks
The select
section contains the list of rules that Ruff checks.
When you don't need to consider some rules, put them in the extend-ignore
section, you can even specify a category similar to the select section.
What I've done
After running ruff check .
I got a bunch of violated rules. Now you can try to fix them all, but my navigator Mark suggests me to solve one single problem at time.
I selected all problems and put them inside extend-ignore
and after that I fixed every rule in a different commit by put inside select
section.
Why choose Ruff?
There's a lot of Linting tools but Ruff is really fast, and it allows to fix some errors automatically.