Editor’s Image
Fail is an extremely fast Python linter and formatter written in Rust that aims to replace and enhance existing tools like Flake8, Black and isort. Provides 10-100x faster performance while maintaining parity through 700+ built-in rules and reimplementation of popular plugins.
Ruff Statistics | Linting the CPython codebase from scratch
Ruff supports modern Python with 3.12 and `pyproject.toml` compatibility. It also offers support for auto-corrections, caching, and editor integrations. Ruff supports monorepo and is used in major open source projects like Pandas, FastAPI, and more. Combining speed, functionality, and usability, Ruff integrates linting, formatting, and autocorrect into a unified tool that’s much faster than existing options.
We can easily install `ruff` 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 on our project. You can also run linter on a single file by replacing “.” with file location.
Ruff has identified 9 bugs and 1 fixable bug. To fix the error, we will use the –fix flag.
As you can see, you have fixed 1 fixable bug.
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 need these tools? The answer is simple: they are beneficial for enforcing coding standards and conventions. As a result, you and your team will be able to focus on the important aspects of your code. Additionally, they help improve the quality, maintainability, and security of our code.
Gif by author
To use Ruff for Jupyter Notebooks in your project, you need to create the `ruff.toml` file and add the following code:
extend-include = ("*.ipynb")
You can also do the same with the `pyproject.toml` file.
After that, run the commands again to see yourself making changes to the Jupyter notebook files.
2 files were reformatted and we have 2 Notebook files.
$ ruff format .
>>> 2 files reformatted, 3 files left unchanged
We also fixed the problems in those files by running the `check` command again.
$ ruff check --fix .
>>> Found 51 errors (6 fixed, 45 remaining).
The final result is amazing. You have made all 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. Review the setting up 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 via `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 fun aspect of Ruff is its VSCode extension. Simplifies formatting and linting, eliminating the need for third-party extensions. Just search Fail in the extension market to install it.
Picture of Ruff – Visual Studio Marketplace
I have set `setting.json` to be formatted on save.
Ruff offers lightning-fast linting and formatting for cleaner, more consistent Python code. With over 700 built-in rules reimplemented in Rust to improve performance, Ruff draws inspiration from popular tools like Flake8, isort, and pyupgrade to apply a comprehensive set of coding best practices. The selected ruleset focuses on catching errors and critical style issues without being overly picky.
Seamless integrations with pre-commit hooks, GitHub Actions, and editors like VSCode make it easy to incorporate Ruff into modern Python workflows. Unmatched speed and carefully designed ruleset make Ruff an essential tool for Python developers who value fast feedback, clean code, and smooth team collaboration. Ruff sets a new standard for Python formatting and linting by combining robust functionality with incredible performance.
Abid Ali Awan (@1abidaliawan) is a certified professional data scientist who loves building machine learning models. Currently, he focuses on content creation and writing technical blogs on data science and machine learning technologies. Abid has a Master’s degree in technology Management and a Bachelor’s degree in Telecommunications Engineering. His vision is to build an artificial intelligence product using a graph neural network for students struggling with mental illness.