Goodbye Flake8, Hello Ruff
Maximizing Code Quality with Ruff: The Fast and Comprehensive Static Code Analysis Tool for Python

Search for a command to run...
Maximizing Code Quality with Ruff: The Fast and Comprehensive Static Code Analysis Tool for Python

No comments yet. Be the first to comment.
Introduction Django-Syzygy is a Django companion that makes database migrations safer in real production environments. It focuses on one specific, painful problem: migrations that are technically valid, but unsafe during rolling deployments. Syzygy w...

Master the core building blocks of Kafka, including partitions, consumer groups, and how to handle tricky topic migrations.

Avoiding Downtime in Django Migrations with django-migration-linter Deploying Django apps with zero downtime is essential in modern production systems, especially when rolling out updates on Kubernetes or deploying worker-based systems incrementally....

Building beautiful, responsive forms in Django doesn’t have to be a painful experience. By combining the power of django-tailwind with django-widget-tweaks and a few well-crafted custom CSS classes, you can create stunning, consistent forms that look...

What is HTMX? HTMX is a powerful tool that allows developers to create highly interactive and dynamic user interfaces without the need for heavy JavaScript frameworks. At its core, HTMX uses HTML attributes to define behaviour, making it an ideal com...

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.
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.
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 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:
[tool.ruff]
# Docs: https://beta.ruff.rs/docs/
# Rules: https://beta.ruff.rs/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 = [
".git",
".mypy_cache",
".pre-commit-cache",
".ruff_cache",
".tox",
".venv",
"venv",
"docs",
"__pycache",
"**/migrations/*",
]
# 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"
[tool.ruff.mccabe]
# 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.
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.
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.1.13'
hooks:
- id: ruff
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.