astral-sh / ruff-pre-commit

A pre-commit hook for Ruff.
Apache License 2.0
802 stars 38 forks source link

Update examples to format first #66

Closed nicoddemus closed 5 months ago

nicoddemus commented 5 months ago

It makes more sense to run ruff formatter fist, as formatting might change the line numbers, which affect the lines reported by the linter.

This is very noticeable when running ruff for the first time on a large number of files, as the formatter will change several lines.

charliermarsh commented 5 months ago

That's a good observation! Though the existing order is fairly intentional. Our linter-formatter compatibility contract is such that running the formatter should not introduce new lint errors -- so if you format after linting (and you haven't enabled any conflicting rules), you should be guaranteed that linting again doesn't yield any new violations.

The inverse is not true. If you run the formatter, then linter with --fix, you might need to format again, since the linter can output code changes that don't adhere to the formatter's code style. So, in general, if you switch the order like this, you can run into a situation where the code is formatted, then fixed (with no remaining lint errors), but then needs to be formatted again.

The formatter-linter compatibility docs are here, and the pre-commit setup is documented here.

nicoddemus commented 5 months ago

I see, thanks for the detailed explanation!

Closing this then. 👍