Learning Notes #70 β RUFF An extremely fast Python linter and code formatter, written in Rust.
In the field of Python development, maintaining clean, readable, and efficient code is needed.
The Ruff Python package is a faster linter and code formatter designed to boost code quality and developer productivity. Written in Rust, Ruff stands out for its blazing speed and comprehensive feature set.
This blog will delve into Ruffβs features, usage, and how it compares to other popular Python linters and formatters like flake8
, pylint
, and black
.
What is Ruff?
Ruff is an extremely fast Python linter and code formatter that provides linting, code formatting, and static code analysis in a single package. It supports a wide range of rules out of the box, covering various Python standards and style guides.
Key Features of Ruff
- Lightning-fast Performance: Written in Rust, Ruff is significantly faster than traditional Python linters.
- All-in-One Tool: Combines linting, formatting, and static analysis.
- Extensive Rule Support: Covers rules from
flake8
,isort
,pyflakes
,pylint
, and more. - Customizable: Allows configuration of rules to fit specific project needs.
- Seamless Integration: Works well with CI/CD pipelines and popular code editors.
Installing Ruff
# Using pip pip install ruff # Using Homebrew (macOS/Linux) brew install ruff # Using UV uv add ruff
Basic Usage
1. Linting a python file
# Lint a single file ruff check app.py # Lint an entire directory ruff check src/
2. Auto Fixing Issues
ruff check src/ --fix
3. Formatting Code
While Ruff primarily focuses on linting, it also handles some formatting tasks
ruff format src/
Configuration
Ruff can be configured using a pyproject.toml
file
[tool.ruff] line-length = 88 exclude = ["migrations"] select = ["E", "F", "W"] # Enable specific rule categories ignore = ["E501"] # Ignore specific rules
Examples
import sys import os print("Hello World !") def add(a, b): result = a + b return a x= 1 y =2 print(x+y) def append_to_list(value, my_list=[]): my_list.append(value) return my_list def append_to_list(value, my_list=[]): my_list.append(value) return my_list
- Identifying Unused Imports
- Auto-fixing Imports
- Sorting Imports
- Detecting Unused Variables
- Enforcing Code Style (PEP 8 Violations)
- Detecting Mutable Default Arguments
- Fixing Line Length Issues
Integrating Ruff with Pre-commit
To ensure code quality before every commit, integrate Ruff with pre-commit
Step 1: Install Pre-Commit
pip install pre-commit
Step 2: Create a .pre-commit-config.yaml
file
repos: - repo: https://github.com/charliermarsh/ruff-pre-commit rev: v0.1.0 # Use the latest version hooks: - id: ruff
Step 3: Install the Pre-commit Hook
pre-commit install
Step 4: Test the Hook
pre-commit run --all-files
This setup ensures that Ruff automatically checks your code for linting issues before every commit, maintaining consistent code quality.
When to Use Ruff
- Large Codebases: Ideal for projects with thousands of files due to its speed.
- CI/CD Pipelines: Reduces linting time, accelerating build processes.
- Code Reviews: Ensures consistent coding standards across teams.
- Open Source Projects: Simplifies code quality management.
- Pre-commit Hooks: Ensures code quality before committing changes.
Integrating Ruff with CI/CD
name: Lint Code on: [push, pull_request] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.10' - name: Install Ruff run: pip install ruff - name: Lint Code run: ruff check .
Ruff is a game-changer in the Python development ecosystem. Its unmatched speed, comprehensive rule set, and ease of use make it a powerful tool for developers aiming to maintain high code quality.
Whether youβre working on small scripts or large-scale applications, Ruff can streamline your linting and formatting processes, ensuring clean, efficient, and consistent code.