Post

πŸš€ Boost Your Code Quality with pre-commit & Ruff – The Ultimate Guide!

πŸš€ Boost Your Code Quality with pre-commit & Ruff – The Ultimate Guide!

Why Use a Linter & Formatter?

Writing clean, consistent, and error-free code is crucial, but manually enforcing style rules is tedious. That’s where linters and formatters come in:

  • πŸ” Linters (like Flake8, Pylint, Ruff) find errors, bad practices, and code smells before they cause issues.

  • 🎨 Formatters (like Black, Ruff) automatically fix code style to keep everything consistent.

By using them, you:

  • βœ… Catch bugs early before they break production

  • βœ… Maintain a clean & readable codebase

  • βœ… Ensure consistency across your team

  • βœ… Save time by automating style enforcement

Tools We Will Use: pre-commit and Ruff

Using automated tools for code checking and formatting helps teams improve code quality and prevent common issues. In this section, we introduce pre-commit and Ruff.

Why Use pre-commit?

pre-commit is a local CI tool that runs before committing your codeβ€”acting as the first line of defense before GitHub Actions or other CI/CD pipelines.

  • βœ… Catches issues early, before they reach the repo

  • βœ… Runs faster than server-side CI (like GitHub Actions) since it works locally

  • βœ… Ensures code quality before pushing

Why Use Ruff Instead of Other Linters & Formatters?

Ruff is a blazing-fast Python linter, formatter, and code analyzer, designed to replace tools like Flake8, Black, isort, pylint, and more. Here’s why it stands out:

  • πŸ”₯ Super Fast β€” Written in Rust, it’s 10–100x faster than traditional Python linters

  • πŸ“¦ All-in-One β€” Combines linting, formatting, and import sorting in a single tool

  • πŸš€ Lightweight β€” Minimal dependencies, making it perfect for large projects

  • πŸ›  Highly Configurable β€” Supports fine-tuned rule selection and customization

How to Configure pre-commit & Ruff

1️⃣ Install Packages

1
    pip install pre-commit ruff

2️⃣ Configure pre-commit

Create a file in your git repo called .pre-commit-config.yaml

1
2
3
4
5
6
7
    repos:
      - repo: https://github.com/astral-sh/ruff-pre-commit
        rev: v0.3.4  # Use the latest version
        hooks:
          - id: ruff
            args: [--fix]  # Be careful, this argument will fix some of the problems
          - id: ruff-format

3️⃣ Configuring ruff

Create a configuration file to define various rules for checking and formatting your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    # pyproject.toml
    [tool.ruff]
    line-length = 88  # Sets the max line length (default is 88, same as Black)
    target-version = "py312"  # Specifies the Python version (py312 for Python 3.12)
    extend-select = [
      "E",  # Style errors (PEP8)
      "F",  # Used variables and syntax errors
      "I",  # Import sorting
      "B",  # Common bugs
      "C",  # Complexity
      "N",  # Naming
      "UP", # Python upgrades
      "S",  # Security
      "RET", # Return statements
      "D"  # Docstrings (Ensures proper documentation)
    ]
    exclude = ["/**/__init__.py"] # Files that you want to ignore

Additional options:

1
2
3
4
5
6
7
8
9
    ignore = ["D100", "D104", "E501"]
    [tool.ruff.per-file-ignores]
    "tests/*" = ["S101"]  # Ignore security warnings in test files
    fixable = ["E", "F", "I", "B"]  # You can also disable auto-fixing for specific rules
    [tool.ruff.isort]
    known-first-party = ["my_project"]  # Treat "my_project" as first-party
    combine-as-imports = true  # Merge "import X as Y" statements
    [tool.ruff.pydocstyle]
    convention = "google"  # Options: "google", "numpy", "pep257"

4️⃣ Install pre-commit Hooks

1
    pre-commit install

5️⃣ Running pre-commit and Checking Code with Ruff πŸ”₯πŸ”₯πŸ”₯

After installation and configuration, you can run pre-commit to check all project files. Running this command may produce an output similar to:

1
2
    Check ruff..............................................................Passed
    Check ruff-format......................................................Passed

If any errors exist, pre-commit will report them, and in some cases, Ruff will automatically fix the issues.

References

For more details, check out these resources:

This post is licensed under CC BY 4.0 by the author.