• Python Home
  • Python Learning Path

Ruff

Ruff is a high performance Python linter and formatter.

Ruff is both a Python tool and Python package. Think of it this way:

Installing Ruff

$ pip install ruff

When you install it, you get the ruff command-line tool:

# To link a directory
$ ruff check .

# To format code
$ ruff format

So technically

ruff (python package)
          |  Installs
  ruff (CLI tool)

What Does Ruff Do?

PurposeTraditional ToolRuff
FormattingBlackYES
LintingFlake8YES
Import SortingisortYES
Unused ImportsPyflakesYES
Code Style Checkspylint pluginsYES
Auto-fixing IssuesAutoflakeYES

Key Features

- pyproject.toml
or
- ruff.toml
or 
- .ruff.toml

Example

Remove Unused Import

Consider this code:

# Filename: app.py

import os
import json

def hello():
    print("hello")

here json is unused.

Now, Run:

$ ruff check app.py

Output:

F401 `json` imported but unused

Lets auto fix it now. Run this code,

$ ruff check --fix app.py

Ruff automatically removes import json so now code will look like

# Filename: app.py

import os

def hello():
    print("hello")

Code Formatting

Ruff can also format code.

Before:

def add(a,b): return a+b

Run:

$ ruff format .

After:

def add(a,b): 
    return a+b

Configuration

Using pyproject.toml :

[tool.ruff]
line-length = 88
target-version = "py3111"

[tool.ruff.format]
quote-style = "single"

[tool.ruff.lint]
select = [
           "E",  # pycodestyle errots
           "F",  # Pyflakes
           "I",  # Import sorting
           "B"   # Bugbear checks
]
ignore = ["E501"] # ignore line-length errors

CI Pipeline

Ruff is commonly run as a quality gate in Github Actions or in any CI pipelines. The idea is simple:

Create:

<your-python-repo>
  .github/
        |___workflows/
            |___ci.yml
   src/
    |____pyproject.toml
name: Python CI

on:
   pull_request:
   push:
       branches:
         - main

jobs:
  quality_gate:
     runs-on: ubuntu-latest
     
     steps:
        - name: Checkout Code
          uses: actions/checkout@v4

        - name: Setup Python
          uses: actions/setup-python@v5
          with:
             python-version: "3.12"

        - name: Install Dependencies
          run: pip install -r requirements.txt

        - name: Ruff Check
          run: ruff check .

        - name: Format Check
          run: ruff format --check .

Extension

Visual Studio Code

The Ruff VS Code Extension is an official extension developed by Astral that integrates the blazing-fast Rust-based Python linter and formatter directly into your editor.

Key Features

To get the most out of the extension, you should configure it to automatically organize imports and format your Python files every time you save. Open your user settings.json file in VS Code and append the following configuration:

{
  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports.ruff": "explicit",
      "source.fixAll.ruff": "explicit"
    }
  }
}

For more details check this link.