Pre-commit hooks and Ruff

ยท

3 min read

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!

๐Ÿ’ก
What's git hook? It's a script that runs at a specific point in the git workflow, with pre-commit it's executed before running 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.

๐Ÿ’ก
Linting involves searching for potential errors. Formatting focuses on making the code visually consistent, such as indentation.

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:

  1. Install Ruff

  2. Run ruff check .

  3. Run ruff check . --fix

  4. 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.

๐Ÿ’ก
What is a rule? It's a code that represents a specific error or a entire category. For example, 'I001' means a specific problem about sorting your imports, but if you want all Isort category solved use simply 'I' as in my example.

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.

Source

Ruff

Pre-commit

My Pull Request

Check DjangoCMS project! and Github repo

ย