Go to file
Charlie Marsh f1ef5ac6b5
Update pre-commit documentation (#57)
I got some feedback on Mastodon that it wasn't clear how to use the
linter and formatter together in pre-commit.

This also removes `--exit-non-zero-on-fix`, since pre-commit already
exits if files are changed, and changes our recommendation to always put
the formatter after the linter, since if a previous tool ends up
committing changes, pre-commit should fail anyway.
2023-11-07 13:33:28 -05:00
.github feat: Update pre-commit mirror script (#53) 2023-09-26 10:38:22 -05:00
.gitignore Initial commit 2022-09-19 20:52:36 -06:00
.pre-commit-hooks.yaml feat: Update pre-commit mirror script (#53) 2023-09-26 10:38:22 -05:00
LICENSE Initial commit 2022-09-19 20:52:36 -06:00
mirror.py feat: Update pre-commit mirror script (#53) 2023-09-26 10:38:22 -05:00
pyproject.toml Mirror: 0.1.4 2023-11-03 21:31:21 +00:00
README.md Update pre-commit documentation (#57) 2023-11-07 13:33:28 -05:00
requirements-dev.txt feat: Update pre-commit mirror script (#53) 2023-09-26 10:38:22 -05:00

ruff-pre-commit

Ruff image image image Actions status

A pre-commit hook for Ruff.

Distributed as a standalone repository to enable installing Ruff via prebuilt wheels from PyPI.

Using Ruff with pre-commit

To run Ruff's linter and formatter (available as of Ruff v0.0.289) via pre-commit, add the following to your .pre-commit-config.yaml:

- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.1.4
  hooks:
    # Run the linter.
    - id: ruff
    # Run the formatter.
    - id: ruff-format

To enable lint fixes, add the --fix argument to the lint hook:

- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.1.4
  hooks:
    # Run the linter.
    - id: ruff
      args: [ --fix ]
    # Run the formatter.
    - id: ruff-format

To run the hooks over Jupyter Notebooks too, add jupyter to the list of allowed filetypes:

- repo: https://github.com/astral-sh/ruff-pre-commit
  # Ruff version.
  rev: v0.1.4
  hooks:
    # Run the linter.
    - id: ruff
      types_or: [ python, pyi, jupyter ]
      args: [ --fix ]
    # Run the formatter.
    - id: ruff-format
      types_or: [ python, pyi, jupyter ]

When running with --fix, Ruff's lint hook should be placed before Ruff's formatter hook, and before Black, isort, and other formatting tools, as Ruff's fix behavior can output code changes that require reformatting.

When running without --fix, Ruff's formatter hook can be placed before or after Ruff's lint hook.

(As long as your Ruff configuration avoids any linter-formatter incompatibilities, ruff format should never introduce new lint errors, so it's safe to run Ruff's format hook after ruff check --fix.)

License

MIT