Goodbye Flake8, Hello Ruff

Goodbye Flake8, Hello Ruff

Maximizing Code Quality with Ruff: The Fast and Comprehensive Static Code Analysis Tool for Python

Static code analysis is an essential part of ensuring the quality and maintainability of your Python code. There are many tools available for static code analysis, but Ruff stands out for its fast performance, comprehensive analysis, and ease of use.

What is Ruff?

Ruff is a static code analysis tool for Python that provides a fast and comprehensive analysis of your code. It checks your code against a set of predefined rules and best practices, helping you to identify and fix problems early in the development cycle. Ruff has a large number of built-in rules covering a wide range of coding standards and best practices, making it a comprehensive tool for ensuring the quality of your code.

Advantages of Ruff

Fast performance: Ruff is written in Rust, a programming language renowned for its performance and efficiency. This makes Ruff much faster than other code analysis tools such as Flake8. Using Rust allows Ruff to quickly analyze even the largest Python projects, giving you fast and accurate results.

Comprehensive analysis: Ruff provides a comprehensive analysis of your code, covering a wide range of best practices and coding standards. This makes it easier to identify and fix issues early in the development cycle before they become bigger problems.

Easy configuration: Ruff allows you to configure your analysis in the pyproject.toml file. This makes it easier to set up and customize your analysis as all the configuration is stored in a single file.

Auto-fix feature: Ruff also has a unique auto-fix feature that allows you to automatically fix common problems with a single command. The --fix option can be used to automatically fix problems that Ruff has identified in your code. This saves you time and effort, allowing you to focus on more important tasks.

Getting started with Ruff

Getting started with Ruff is simple. You can install Ruff using pip, the Python package manager. Just run the following command in your terminal

pip install ruff

Once Ruff is installed, you can configure your analysis in the pyproject.toml file. Here is an example configuration:

# Docs:
# Rules:
select = ["B", "C4", "EXE", "F", "E", "ISC", "ICN", "INP", "PIE", "SIM", "W", "T20", "UP", "T10", "G", "C90", "ERA"]
ignore = ["B008", "SIM102"]
fixable = ["F", "E", "B", "C4", "EXE", "ISC", "ICN", "INP", "PIE", "SIM", "W", "T20", "UP"]
unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [

# Same as Black.
line-length = 100

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
# Assume Python 3.11.
target-version = "py311"

# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10

For a full list of Ruff rules check it out here.

To run Ruff, simply navigate to your project directory in the terminal and run the following command:

ruff check .

Ruff will analyze your code and display any errors or warnings it has found. If you want to automatically fix common issues, you can use the --fix option:

ruff check . --fix

Ruff will automatically fix any problems it finds, saving you time and effort.

VSCode extension

Ruff also has a Visual Studio Code (VSCode) extension that makes it even easier to use. The VSCode extension provides real-time analysis of your code, allowing you to quickly identify and fix problems.

Pre-commit integration

- repo:
  # Ruff version.
  rev: 'v0.1.13'
    - id: ruff

The bottom line

In conclusion, Ruff is a fast and powerful static code analysis tool for Python. Its fast performance, comprehensive analysis and easy configuration make it an excellent choice for any Python project looking to improve code quality and avoid common pitfalls.