Skip to content

Use Ruff for linting and code formatting #739

@tiran

Description

@tiran

Quote from Ruff

An extremely fast Python linter and code formatter, written in Rust.

Ruff is a fantastic tool that combines the features of flake8 linter, isort, Black code formatter, and (to some degree) pylint in one tool. It comes with over 800 linting rules. Ruff is not only blazing fast, it can also fix most code violation. The code fixer is a fantastic quality of life feature for developers. I have been using Ruff in my projects for a while and haven't had a major problem, yet.

I'm not the only person that prefers Ruff. @thrix has started PR #519. IMHO the PR is doing a bit too ambitious and changes too much at once. I recommend to aim lower and start with fewer checkers, or even no checker and just code formatting.

Black

We can use Ruff as a full replacement for Black with this configuration:

[tool.ruff]
# Same as Black.
line-length = 88
target-version = "py39"

isort (import sorter)

The project configures isort to insert headings. The import_heading_* options are one of few features lacking in Ruff. If we want to keep the headings, then we need to keep using isort for now. Heading settings feature request: astral-sh/ruff#6371

pylint

pylint can perform checks that are not implemented in Ruff, e.g. AST inference and plugins. The downside of pylint is that it's very slow and that all dependencies must be installed first. Even with some tricks, a tox virtual env with Torch and other dependencies is > 1.5 GiB.

Ruff's linters work differently and do not require dependencies to be installed. At the moment, we have only a few pylint checks enabled. It looks like we can replace most to all checks with Ruff + get tons of additional checks for free.

https://docs.astral.sh/ruff/faq/#how-does-ruffs-linter-compare-to-pylint

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions