Image by Editor
Ruff is an extremely fast Python linter and formatter written in Rust that aims to replace and improve upon existing tools like Flake8, Black, and isort. It provides 10-100x faster performance while maintaining parity through over 700 built-in rules and reimplementation of popular plugins.
Stats from Ruff | Linting the CPython codebase from scratch
Ruff supports modern Python with 3.12 compatibility and `pyproject.toml`. It also offers automatic fix support, caching, and editor integrations. Ruff is monorepo-friendly and used in major open-source projects like Pandas, FastAPI, and more. By combining speed, functionality, and usability, Ruff integrates linting, formatting, and automatic fixing in a unified tool that is orders of magnitude faster than existing options.
We can easily install `ruff` by using PIP.
To test how easy and fast it is to run Ruff, we can use the DagHub repository kingabzpro/Yoga-Pose-Classification. You can clone it or use your own project to format.
Project Structure
First, we will run a linter over our project. You can also run linter on a single file by replacing “.” with file location.
Ruff has identified 9 errors and 1 fixable error. To fix the error, we will use the –fix flag.
As you can see, it has fixed the 1 fixable error.
To format the project, we will use the `ruff format` command.
$ ruff format .
>>> 3 files reformatted
The Ruff linter and formatter have made numerous changes to the code. But, why do we require these tools? The answer is simple – they are beneficial in enforcing coding standards and conventions. As a result, both you and your team can concentrate on the significant aspects of your code. Moreover, they help enhance the quality, maintainability, and security of our code.
Gif by Author
To use Ruff for Jupyter Notebooks in the project, you have to create `ruff.toml` file and add the following code:
extend-include = ["*.ipynb"]
You can also do the same with the `pyproject.toml` file.
After that re-run the commands to see it making changes to Jupyter notebook files.
2 files were reformatted and we have 2 Notebook files.
$ ruff format .
>>> 2 files reformatted, 3 files left unchanged
We have also fixed the issues in those files by running the `check` command again.
$ ruff check --fix .
>>> Found 51 errors (6 fixed, 45 remaining).
The final result is amazing. It has made all of the necessary changes without breaking the code.
Gif by Author
It’s easy to configure Ruff for Jupyter Notebooks by editing the `ruff.toml` file to adjust the linter and formatter settings. Check out the configuring Ruff documentation for more details.
target-version = "py311"
extend-include = ["*.ipynb"]
line-length = 80
[lint]
extend-select = [
"UP", # pyupgrade
"D", # pydocstyle
]
[lint.pydocstyle]
convention = "google"
Developers and teams can use Ruff as a pre-commit hook through the `ruff-pre-commit`:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.5
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
It can also be used as a GitHub Action via `ruff-action`:
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
The most enjoyable aspect of Ruff is its VSCode extension. It simplifies formatting and linting, eliminating the need for third-party extensions. Simply search for Ruff on the extension marketplace to install it.
Image from Ruff – Visual Studio Marketplace
I have configured `setting.json` so that it formats on save.
Ruff delivers lightning-fast linting and formatting for cleaner, more consistent Python code. With over 700 built-in rules reimplemented in Rust for performance, Ruff draws inspiration from popular tools like Flake8, isort, and pyupgrade to enforce a comprehensive set of coding best practices. The curated ruleset focuses on catching bugs and critical style issues without excessive nitpicking.
Seamless integrations with pre-commit hooks, GitHub Actions, and editors like VSCode make incorporating Ruff into modern Python workflows easy. The unmatched speed and thoughtfully designed ruleset make Ruff an essential tool for Python developers who value rapid feedback, clean code, and smooth team collaboration. Ruff sets a new standard for Python linting and formatting by combining robust functionality with blazing performance.
Abid Ali Awan (@1abidaliawan) is a certified data scientist professional who loves building machine learning models. Currently, he is focusing on content creation and writing technical blogs on machine learning and data science technologies. Abid holds a Master’s degree in Technology Management and a bachelor’s degree in Telecommunication Engineering. His vision is to build an AI product using a graph neural network for students struggling with mental illness.