From f1ef5ac6b5a6eadcd37ac6fbaf7803460fb1ca33 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 7 Nov 2023 10:33:28 -0800 Subject: [PATCH 1/2] 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. --- README.md | 54 +++++++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b892b25..2d390ff 100644 --- a/README.md +++ b/README.md @@ -13,67 +13,59 @@ Distributed as a standalone repository to enable installing Ruff via prebuilt wh ### Using Ruff with pre-commit -Add this to your `.pre-commit-config.yaml`: +To run Ruff's [linter](https://docs.astral.sh/ruff/linter) and [formatter](https://docs.astral.sh/ruff/formatter) +(available as of Ruff v0.0.289) via pre-commit, add the following to your `.pre-commit-config.yaml`: ```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 ``` -Or, to enable autofix: +To enable lint fixes, add the `--fix` argument to the lint hook: ```yaml - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. rev: v0.1.4 hooks: + # Run the linter. - id: ruff - args: [--fix, --exit-non-zero-on-fix] + args: [ --fix ] + # Run the formatter. + - id: ruff-format ``` -To run the hook on Jupyter Notebooks too: +To run the hooks over Jupyter Notebooks too, add `jupyter` to the list of allowed filetypes: ```yaml - 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] -``` - -Ruff's pre-commit hook should be placed after other formatting tools, such as Black and isort, -_unless_ you enable autofix, in which case, Ruff's pre-commit hook should run _before_ Black, isort, -and other formatting tools, as Ruff's autofix behavior can output code changes that require -reformatting. - -### Using Ruff's formatter (unstable) - -[Ruff's formatter](https://docs.astral.sh/ruff/formatter) can used with pre-commit by adding the `ruff-format` hook: - -```yaml -- repo: https://github.com/astral-sh/ruff-pre-commit - # Ruff version. - rev: v0.1.4 - hooks: + types_or: [ python, pyi, jupyter ] + args: [ --fix ] + # Run the formatter. - id: ruff-format + types_or: [ python, pyi, jupyter ] ``` -To check formatting without changing files, use `--check`: +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. -```yaml -- repo: https://github.com/astral-sh/ruff-pre-commit - # Ruff version. - rev: v0.1.4 - hooks: - - id: ruff-format - args: [--check] -``` +When running without `--fix`, Ruff's formatter hook can be placed before or after Ruff's lint hook. -Note `v0.0.290` is the minimum version that provides the `ruff-format` hook. +(As long as your Ruff configuration avoids any [linter-formatter incompatibilities](https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules), +`ruff format` should never introduce new lint errors, so it's safe to run Ruff's format hook _after_ +`ruff check --fix`.) ## License From 3d440bd6c67653f33f1c89ff9d6d450685ef9bfc Mon Sep 17 00:00:00 2001 From: Github Actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 8 Nov 2023 23:12:19 +0000 Subject: [PATCH 2/2] Mirror: 0.1.5 --- README.md | 12 ++++++------ pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2d390ff..ad7253e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # ruff-pre-commit [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) -[![image](https://img.shields.io/pypi/v/ruff/0.1.4.svg)](https://pypi.python.org/pypi/ruff) -[![image](https://img.shields.io/pypi/l/ruff/0.1.4.svg)](https://pypi.python.org/pypi/ruff) -[![image](https://img.shields.io/pypi/pyversions/ruff/0.1.4.svg)](https://pypi.python.org/pypi/ruff) +[![image](https://img.shields.io/pypi/v/ruff/0.1.5.svg)](https://pypi.python.org/pypi/ruff) +[![image](https://img.shields.io/pypi/l/ruff/0.1.5.svg)](https://pypi.python.org/pypi/ruff) +[![image](https://img.shields.io/pypi/pyversions/ruff/0.1.5.svg)](https://pypi.python.org/pypi/ruff) [![Actions status](https://github.com/astral-sh/ruff-pre-commit/workflows/main/badge.svg)](https://github.com/astral-sh/ruff-pre-commit/actions) A [pre-commit](https://pre-commit.com/) hook for [Ruff](https://github.com/astral-sh/ruff). @@ -19,7 +19,7 @@ To run Ruff's [linter](https://docs.astral.sh/ruff/linter) and [formatter](https ```yaml - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.1.4 + rev: v0.1.5 hooks: # Run the linter. - id: ruff @@ -32,7 +32,7 @@ To enable lint fixes, add the `--fix` argument to the lint hook: ```yaml - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.1.4 + rev: v0.1.5 hooks: # Run the linter. - id: ruff @@ -46,7 +46,7 @@ To run the hooks over Jupyter Notebooks too, add `jupyter` to the list of allowe ```yaml - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.1.4 + rev: v0.1.5 hooks: # Run the linter. - id: ruff diff --git a/pyproject.toml b/pyproject.toml index c3407a7..efcd1d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "ruff-pre-commit" version = "0.0.0" dependencies = [ - "ruff==0.1.4", + "ruff==0.1.5", ] [project.optional-dependencies]