What is Ruff?
Ruff is a lightning-fast Python linter and formatter built in Rust. It combines the functionality of popular tools like isort, flake8, and pyflakes into a single, high-performance package, making it easy to format and lint your entire codebase without managing multiple configurations. By default, Ruff follows many of the style rules defined in PEP 8, Python’s official style guide, helping teams maintain clean, consistent, and readable code across projects.How to Install it?
Install ruff from PyPi. This will add ruff to your pyproject.toml file as a dev dependency.How to lint?
In order to use ruff to lint your repository, run the command below. This will check your entire directory recursively if there are any lint warnings or errors.. with the directory or filename. The example below will run lint on the tests directory.
Configuration
By default, ruff has rules to format your code. If you would like to customize this, add specific rules to your pyproject.toml file.Fixing your code
Ruff will fix your code to adhere to the rules. This will attempt at fixing fixable issues based on the rules you have defined.--unsafe-fixes argument. This will not fix all your issues but the issues ruff can resolve which may be unsafe to fix automatically.
Formatting
You can add specific formatting configuration which ruff can fix for you.Auto Format
You can setup ruff to format your code automatically. This will look for file changes and format your code when a file is changed..vscode/settings.json file.