|
| 1 | +# Enforcing Coding Standards with Ruff |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +[Ruff](https://github.com/astral-sh/ruff) is the fastest Python linter and formatter that consolidates various tools like `pyflakes`, `pyupgrade`, `isort`, `black`, `mccabe`, `bandit`, and more. It helps maintain consistent coding standards, ensures security, and optimizes imports automatically. |
| 6 | + |
| 7 | +## Steps to Integrate Ruff in Your Project |
| 8 | + |
| 9 | +### 1. Install Ruff |
| 10 | +You can install Ruff using `pip` or by adding it to your `requirements.txt` file: |
| 11 | + |
| 12 | +```sh |
| 13 | +pip install ruff |
| 14 | +``` |
| 15 | + |
| 16 | +Or, add the following line to `requirements.txt`: |
| 17 | + |
| 18 | +``` |
| 19 | +ruff==0.9.2 |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Add `.ruff_cache` to `.gitignore` |
| 23 | +The `.ruff_cache` directory stores cache files related to Ruff. To avoid committing unnecessary files, add the following line to your `.gitignore`: |
| 24 | + |
| 25 | +``` |
| 26 | +.ruff_cache |
| 27 | +``` |
| 28 | + |
| 29 | +### 3. Configure Ruff |
| 30 | +Create a `ruff.toml` file in the root of your project and add the following configuration: |
| 31 | + |
| 32 | +```toml |
| 33 | +# Target Python version |
| 34 | +[tool.ruff] |
| 35 | +target-version = "py313" |
| 36 | + |
| 37 | +# Enable additional linting rules |
| 38 | +[lint] |
| 39 | +extend-select = [ |
| 40 | + "UP", # pyupgrade |
| 41 | + "I", # isort |
| 42 | + "C90", # complexity |
| 43 | + "N", # pep8-naming |
| 44 | + "ASYNC", # flake8-async |
| 45 | + "S", # flake8-bandit |
| 46 | + "B", # flake8-bugbear |
| 47 | + "A", # flake8-builtins |
| 48 | + "C4", # flake8-comprehensions |
| 49 | +] |
| 50 | + |
| 51 | +# Ignore specific rules for test files |
| 52 | +[lint.per-file-ignores] |
| 53 | +"tests/*.py" = ["S101"] # Ignore assert statements in tests |
| 54 | +``` |
| 55 | + |
| 56 | +Refer to the following links for more details on Ruff rules and settings: |
| 57 | +- [Ruff Rules](https://docs.astral.sh/ruff/rules) |
| 58 | +- [Ruff Settings](https://docs.astral.sh/ruff/settings/#lint_unfixable) |
| 59 | +- [Flake8 Rules](https://www.flake8rules.com/) |
| 60 | + |
| 61 | +### 4. Run Ruff Checks |
| 62 | +Once Ruff is installed and configured, run the following commands to check and fix linting issues: |
| 63 | + |
| 64 | +```sh |
| 65 | +# Check for linting errors |
| 66 | +ruff check |
| 67 | + |
| 68 | +# Automatically fix fixable errors |
| 69 | +ruff check --fix |
| 70 | + |
| 71 | +# Format code according to standards |
| 72 | +ruff format |
| 73 | +``` |
| 74 | + |
| 75 | +> **ℹ️ Info:** The pre-commit step is WIP. Please don't follow it yet. |
| 76 | +### 5. Enforce Ruff in Pre-Commit Hooks |
| 77 | +To prevent committing unformatted or non-linted code, integrate Ruff with `pre-commit`. |
| 78 | + |
| 79 | +#### Install `pre-commit` |
| 80 | + |
| 81 | +If you haven't installed `pre-commit`, install it using: |
| 82 | + |
| 83 | +```sh |
| 84 | +pip install pre-commit |
| 85 | +``` |
| 86 | + |
| 87 | +#### Update `.pre-commit-config.yaml` |
| 88 | + |
| 89 | +Add the following configuration to your `.pre-commit-config.yaml` file: |
| 90 | + |
| 91 | +```yaml |
| 92 | +- repo: https://github.com/astral-sh/ruff-pre-commit |
| 93 | + rev: v0.1.4 |
| 94 | + hooks: |
| 95 | + - id: ruff |
| 96 | + args: [ --fix ] |
| 97 | + - id: ruff-format |
| 98 | +``` |
| 99 | +
|
| 100 | +#### Install Pre-Commit Hooks |
| 101 | +
|
| 102 | +Run the following command to install the newly added pre-commit hooks: |
| 103 | +
|
| 104 | +```sh |
| 105 | +pre-commit install |
| 106 | +``` |
| 107 | + |
| 108 | +### 6. Handling Pre-Commit Failures |
| 109 | +If Ruff detects issues during a commit, it will prevent the commit until the issues are fixed. Follow these steps to resolve the errors: |
| 110 | + |
| 111 | +1. Review the errors printed by Ruff. |
| 112 | +2. Run `ruff check --fix` to auto-fix issues. |
| 113 | +3. Run `ruff format` to format the code. |
| 114 | +4. Add the modified files to staging: |
| 115 | + |
| 116 | + ```sh |
| 117 | + git add . |
| 118 | + ``` |
| 119 | + |
| 120 | +5. Retry committing: |
| 121 | + |
| 122 | + ```sh |
| 123 | + git commit -m "Fix linting issues" |
| 124 | + ``` |
| 125 | + |
| 126 | +### 7. Integrate Ruff in IDEs |
| 127 | +To ensure code is formatted properly while writing, install the Ruff extension in your IDE: |
| 128 | + |
| 129 | +- **PyCharm Plugin**: [Install from JetBrains Marketplace](https://plugins.jetbrains.com/plugin/20574-ruff) |
| 130 | +- **VSCode Extension**: [Install from Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) |
| 131 | + |
| 132 | +## Conclusion |
| 133 | +By integrating Ruff into your workflow, you ensure consistent, optimized, and secure code. Implementing Ruff with `pre-commit` enforces coding standards and prevents unformatted code from being committed. Start using Ruff today to maintain high-quality Python code! |
| 134 | + |
0 commit comments