What is linting? A linter will help you find syntactical and stylistic issues with your code. Python has several great 3rd party linting tools.
Here are the two most famous ones:
Innumerable projects use Flake8 to help find problems in their code before it is committed. Many also use pylint, but you won’t hear or see it used quite often.
Flake8 wraps PyFlakes, pycodestyle, and Ned Batchelder’s McCabe Script, giving you a few extras that pylint may not have.
Introducing Ruff
Ruff was released in the past year and has been gaining a lot of popularity. Ruff’s claim to fame is that it is fast because it was written in Rust! Ruff is 10-100x faster than existing linters!
Ruff has near-parity with the built-in Flake8 rule set and 500+ rules that reimplement many of Flake8’s popular plugins.
Installing Ruff
You can install Ruff using pip:
pip install ruff
If you are on Mac or Linux and you use Brew, you can use that too:
brew install ruff
Ruff is also installable with conda:
conda install -c conda-forge ruff
Let’s find out how you might use Ruff on your own codebase!
Using Ruff
Now that you have Ruff installed, let’s look at a few examples of running Ruff on your code base!
Here are some examples from the Ruff docs:
# Lint all files in the current directory (and any subdirectories)
ruff check .
# Lint all files in `/path/to/code` (and any subdirectories)
ruff check path/to/code/
# Lint all `.py` files in `/path/to/code`
ruff check path/to/code/*.py
# Lint `file.py`
ruff check path/to/code/to/file.py
Ruff also supports a handy mode to watch for changes on a folder and re-run when a change is detected.
Here is the command you can use for that:
ruff check path/to/code/ --watch
Ruff Also Replaces Black and isort
You can also configure Ruff to replace Black and isort. The sorting is slightly different then isort, which you can read about in the Ruff FAQ.
Wrapping Up
Ruff is still in beta, but it looks like an exciting project. You should definitely check it out and see what you can do with it!