Ruff: The ‘Black and Flake8 Killer’ That Speeds Up Python Projects 100x

Python tutorial - IT technology blog
Python tutorial - IT technology blog

The Nightmare of Waiting for Linting and Formatting

If you’re managing a large Python project, this scenario is likely familiar: you finish writing code, hit Save, and then wait 3-5 seconds for Black to reformat it. Next comes CI/CD, which takes several more minutes just for Flake8 to check for logic errors.

When I first started working on projects with hundreds of files, I considered this waiting time a given. But after six months of switching entirely to Ruff in production, I realized how much time I had been wasting. Ruff isn’t just another tool; it completely changes how we maintain code quality thanks to the power of Rust.

Why Should You Ditch Old Tools for Ruff?

1. Blazing Fast Speed

Ruff is unbelievably fast. According to official benchmarks, it’s 10 to 100 times faster than Flake8. In practice, for a repo with about 500 files, Flake8 takes nearly 10 seconds to complete, while Ruff finishes in exactly 0.18 seconds. The feeling of hitting Ctrl + S and seeing your code snap into the correct format instantly is truly addictive.

2. Everything in One Place (All-in-one)

Previously, my configuration files were cluttered: .flake8, pyproject.toml (for Black), and then .isort.cfg. Ruff eliminates this bloat. With a single tool, you can completely replace:

  • Flake8: Checks for logic errors and code style.
  • Black: Automatically formats code according to standards.
  • isort: Organizes import lines neatly.
  • pyupgrade: Automatically upgrades syntax to the latest Python version.

3. Intelligent Autofix Capabilities

Ruff doesn’t just point out problems from the sidelines. It can automatically fix many common errors. By simply adding the --fix flag, Ruff will remove redundant imports, fix unused variables, or upgrade old syntax to Python 3.10+. About 80% of warnings will vanish without you touching the keyboard.

How to Implement Ruff in Real-World Projects

To get started, you don’t need to delete everything immediately. Install and experience it alongside your current tools by following these steps.

Step 1: Super Simple Installation

You can install it via pip or package managers like Poetry:

pip install ruff

If you’re using macOS and want to use it for all projects on your machine:

brew install ruff

Step 2: Centralized Configuration in pyproject.toml

I always recommend putting the configuration in pyproject.toml. This keeps the project much cleaner. Here is the config set I’m applying to real-world projects:

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

[tool.ruff.lint]
# E, F: Basic Flake8
# I: isort (import sorting)
# B: flake8-bugbear (finding potential logic errors)
# UP: pyupgrade (syntax upgrades)
select = ["E", "F", "I", "B", "UP", "N"]
ignore = ["E501"] # Let the formatter handle long lines

[tool.ruff.format]
quote-style = "double"
indent-style = "space"

Step 3: Essential Commands to Remember

To scan the entire project for errors, type:

ruff check .

To scan and automatically fix errors (like removing unused imports):

ruff check . --fix

And to format code (completely replacing Black):

ruff format .

Pro-Tip: Don’t Skip This Step

A small tip when working with complex Ruff rules, especially errors related to Regex strings: sometimes Ruff will report escape sequence errors if you write non-standard regex. My experience is to quickly test patterns at toolcraft.app/en/tools/developer/regex-tester before putting them into code. This helps you determine if the error lies in the regex logic or if the Ruff configuration is being too strict.

Integrate into VS Code for a Smoother Workflow

Install the official Ruff extension by Astral. Then, add the following to your settings.json so that every time you press Save, your code is automatically cleaned up:

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

Optimize CI/CD with GitHub Actions

Because Ruff is extremely lightweight, it significantly saves pipeline execution time. Here is the snippet I often use, which takes only a few seconds to complete checks for each Pull Request:

name: Lint
on: [push, pull_request]
jobs:
  ruff:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/ruff-action@v1
        with:
          args: "check ."

Conclusion: Should You Switch Today?

If you’re starting a new project, don’t hesitate: Use Ruff from the start. It makes the development environment cleaner and many times faster.

For existing projects running stably, you can migrate gradually. Ruff provides the ruff check --add-noqa command to automatically tag and ignore existing errors. This allows you to apply Ruff immediately without having to manually fix thousands of lines of legacy code.

Trust me, once you get used to the speed of Rust, you’ll never want to go back to the days of waiting for Black or Flake8 to run.

Share: