# Ruff
An Python [[Linter]] and [[Code Formatter]], written in [[Rust]].
```shell
pip install ruff
```
| Caracteristic | |
| ------------- | ------------------------------------------------------------------------ |
| Language | [[Rust]] |
| Maintainer | [[Astral]] |
| License | [MIT License (MIT)](https://github.com/astral-sh/ruff/blob/main/LICENSE) |
| Homepage | [Github](https://github.com/astral-sh/ruff) |
| Documentation | [Docs](https://docs.astral.sh/ruff/) |

## Configuration
It is possible to use configuration files and [[CLI - Command Line Interface|CLI]].[^1]
Common configuration files used are [[pyproject.toml]] or [[ruff.toml]] file:
```toml
[tool.ruff]
target-version = "py313"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "N", "D", "I"]
exclude = ["D100", "D104", "D106", "D107"]
```
Using [[CLI - Command Line Interface|CLI]] options:
```sh
ruff check --select F,N,D path/to/code/
```
Here is an example to enable a rule.
## Rules
[WIP]
From Ruff 0.10.0:
| Code | Type | Name | From | Stable | Auto-fix |
| ------------------------------------- | ----------------------------------------- | ---------------------------- | ----------- | ------ | -------- |
| [[Redundant response model\|FAST001]] | [[Anti-Patterns of Python\|anti-pattern]] | [[Redundant response model]] | [[FastAPI]] | Yes | No |
| [[Non-annotated dependency\|FAST002]] | [[Anti-Patterns of Python\|anti-pattern]] | [[Non-annotated dependency]] | [[FastAPI]] | Yes | Yes |
| [[Unused path parameter\|FAST003]] | [[Software defects of Python\|defect]] | [[Unused path parameter]] | [[FastAPI]] | No | Yes |
## Rollout Strategy
This strategy aims to a smooth transition to Ruff in projects at various scales. It can be targeted to a specific team e.g. for testing or canary rollout, or an entire organization with [[CICD - Continuous Integration Continuous Deployment|CICD]] tools such as [[R2Devops]].
### Required configuration
Required configuration is for everything that is from default Python or security related, even libraries that are not actively used (such as gettext and others), as is might be harder to detect usage compared to exernal libraries that would be referenced as dependencies in pyproject.toml or requirements.txt.
#### 1. Security and reliability
- flake8-bandit (S)
- pygrep-hooks (PGH)
#### 2. Software defects
##### Rollout 1
- flake8-bugbear (B)
- flake8-blind-except (BLE)
##### Rollout 2
- flake8-logging (LOG)
- flake8-logging-format (G)
##### Rollout 3
- flake8-2020 (YTT)
- flake8-gettext (INT)
- flake8-no-pep420 (INP)
##### Rollout 4
- mccabe (C90) (max-complexity = 30)
- Perflint (PERF)
#### 3. Updates and deadcode
- eradicate (ERA)
- flake8-debugger (T10)
- flake8-future-annotations (FA)
- flake8-use-pathlib (PTH)
- pyupgrade (UP)
#### 4. Conventions
- flake8-import-conventions (ICN)
- flake8-self (SLF)
- isort (I)
- pep8-naming (N)
- pycodestyle (E, W) (Define formatter conflicting rules)
#### 5. Quality and anti-patterns
##### Rollout 1
- flake8-annotations (ANN)
- flake8-async (ASYNC)
- flake8-return (RET)
- mccabe (C90) (max-complexity = 20)
##### Rollout 2
- flake8-boolean-trap (FBT)
- flake8-builtins (A)
- flake8-unused-arguments (ARG)
- mccabe (C90) (max-complexity = 17)
##### Rollout 3
- flake8-implicit-str-concat (ISC)
- flake8-comprehensions (C4)
- flake8-raise (RSE)
- mccabe (C90) (max-complexity = 15)
##### Rollout 4
- flake8-commas (COM)
- flake8-datetimez (DTZ)
- flake8-errmsg (EM)
- mccabe (C90) (max-complexity = 12)
##### Rollout 5
- flake8-pyi (PYI)
- mccabe (C90) (max-complexity = 10)
##### Rollout 6
- flake8-pie (PIE)
- flake8-implicit-str-concat (ISC)
- flake8-executable (EXE)
- flake8-print (T20)
##### Rollout 7
- flake8-fixme (FIX) (excluded: FIX002(see TD))
- flake8-todos (TD)
- flake8-simplify (SIM)
- flake8-slots (SLOT)
##### Rollout 8
- banned-api (TID251)
- flake8-type-checking (TC)
- flynt (FLY)
##### Rollout 9
- pydocstyle (D)
##### Backlog
### Optional configuration
Optional configuration is for easy to detect external dependencies.
#### 1. Security and reliability
#### 2. Software defects
#### 3. Updates and deadcode
#### 4. Quality and anti-patterns
- FastAPI (FAST)
- flake8-django (DJ)
- NumPy-specific rules (NPY)
- pandas-vet (PD)
- Airflow (AIR)
### Excluded configuration
Rules that should be excluded in any case.
- flake8-quotes (Q): addressed by ruff formatter.
### Triage
- Pyflakes (F)
- Pylint (PL)
- refurb (FURB)
- Ruff-specific rules (RUF)
- tryceratops (TRY)
## CICD
[[Need content]]
---
Bibliography:
- [The Ruff Formatter](https://astral.sh/blog/the-ruff-formatter)
[^1]: [Configuring Ruff - docs.astral.sh](https://docs.astral.sh/ruff/configuration/)