|
| 1 | +# Design Document |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This design outlines the migration from Black and isort to Ruff for the We All Code Django project. Ruff is a fast Python linter and formatter written in Rust that can replace both Black (formatting) and isort (import sorting) with a single tool. The migration will maintain existing code style preferences while consolidating tooling and improving performance. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Tool Replacement Strategy |
| 10 | + |
| 11 | +The migration follows a direct replacement approach: |
| 12 | + |
| 13 | +- **Black** → **Ruff formatter** (maintains Black-compatible formatting) |
| 14 | +- **isort** → **Ruff import sorting** (maintains isort-compatible import organization) |
| 15 | + |
| 16 | +### Configuration Approach |
| 17 | + |
| 18 | +Ruff will be configured in `pyproject.toml` using the `[tool.ruff]` section with subsections for: |
| 19 | + |
| 20 | +- General settings (line length, target Python version, exclusions) |
| 21 | +- Formatter settings (`[tool.ruff.format]`) |
| 22 | +- Import sorting settings (`[tool.ruff.isort]`) |
| 23 | +- Linting rules (`[tool.ruff.lint]`) |
| 24 | + |
| 25 | +## Components and Interfaces |
| 26 | + |
| 27 | +### Configuration Files |
| 28 | + |
| 29 | +#### pyproject.toml Updates |
| 30 | + |
| 31 | +- Remove `[tool.black]` section |
| 32 | +- Remove `[tool.isort]` section |
| 33 | +- Add comprehensive `[tool.ruff]` configuration |
| 34 | +- Add Ruff as a development dependency in the "# Development & Debugging" section alongside django-debug-toolbar |
| 35 | + |
| 36 | +#### Pre-commit Configuration Updates |
| 37 | + |
| 38 | +- Replace Black hook (currently using psf/black rev 23.3.0) with Ruff formatter hook |
| 39 | +- Replace isort hook (currently using pycqa/isort rev 5.12.0) with Ruff import sorting hook |
| 40 | +- Maintain existing exclusion patterns for migrations and .vscode folders |
| 41 | +- Keep all other pre-commit hooks unchanged (trailing-whitespace, end-of-file-fixer, etc.) |
| 42 | + |
| 43 | +#### Documentation Updates |
| 44 | + |
| 45 | +- Update `.kiro/steering/tech.md` Code Quality Tools section to reference Ruff instead of Black and isort |
| 46 | +- Update `.kiro/steering/structure.md` Development Conventions section to reference Ruff formatting |
| 47 | +- Check and update `README.md` if it contains references to Black or isort |
| 48 | +- Update any developer setup instructions to include Ruff-specific commands |
| 49 | +- Ensure all documentation maintains consistency with Ruff usage and 79-character line length |
| 50 | + |
| 51 | +### Ruff Configuration Sections |
| 52 | + |
| 53 | +Based on the current Black and isort configuration, Ruff will be configured as follows: |
| 54 | + |
| 55 | +#### Core Settings |
| 56 | + |
| 57 | +```toml |
| 58 | +[tool.ruff] |
| 59 | +line-length = 79 |
| 60 | +target-version = "py311" |
| 61 | +exclude = [migrations, build artifacts, etc.] |
| 62 | +``` |
| 63 | + |
| 64 | +#### Formatter Settings |
| 65 | + |
| 66 | +```toml |
| 67 | +[tool.ruff.format] |
| 68 | +quote-style = "double" |
| 69 | +indent-style = "space" |
| 70 | +skip-source-first-line = false |
| 71 | +line-ending = "auto" |
| 72 | +``` |
| 73 | + |
| 74 | +#### Import Sorting Settings |
| 75 | + |
| 76 | +```toml |
| 77 | +[tool.ruff.isort] |
| 78 | +known-django = ["django"] |
| 79 | +section-order = ["future", "standard-library", "django", "third-party", "first-party", "local-folder"] |
| 80 | +combine-as-imports = true |
| 81 | +force-wrap-aliases = true |
| 82 | +split-on-trailing-comma = true |
| 83 | +``` |
| 84 | + |
| 85 | +#### Linting Rules |
| 86 | + |
| 87 | +```toml |
| 88 | +[tool.ruff.lint] |
| 89 | +select = ["E", "F", "W", "I"] # Basic error, warning, and import rules |
| 90 | +ignore = [] # Project-specific ignores if needed |
| 91 | +``` |
| 92 | + |
| 93 | +## Data Models |
| 94 | + |
| 95 | +No data models are affected by this migration as it only changes development tooling configuration. |
| 96 | + |
| 97 | +## Error Handling |
| 98 | + |
| 99 | +### Migration Validation |
| 100 | + |
| 101 | +- Verify Ruff produces equivalent formatting to Black on existing codebase |
| 102 | +- Ensure import sorting maintains Django-aware section organization |
| 103 | +- Test pre-commit hooks function correctly with new configuration |
| 104 | + |
| 105 | +### Rollback Strategy |
| 106 | + |
| 107 | +- Keep backup of original Black/isort configuration |
| 108 | +- Document steps to revert if issues are discovered |
| 109 | +- Maintain git history for easy rollback |
| 110 | + |
| 111 | +## Testing Strategy |
| 112 | + |
| 113 | +### Configuration Testing |
| 114 | + |
| 115 | +1. **Format Consistency Test**: Run Ruff formatter on existing codebase and verify minimal changes |
| 116 | +2. **Import Sorting Test**: Verify Ruff import sorting maintains Django section organization |
| 117 | +3. **Pre-commit Integration Test**: Test pre-commit hooks with Ruff configuration |
| 118 | +4. **Exclusion Pattern Test**: Verify migrations and other excluded files are not processed |
| 119 | + |
| 120 | +### Validation Steps |
| 121 | + |
| 122 | +1. Install Ruff and configure in pyproject.toml |
| 123 | +2. Run `ruff format --check` on codebase to verify compatibility |
| 124 | +3. Run `ruff check --select I` to test import sorting |
| 125 | +4. Test pre-commit hooks in isolated environment |
| 126 | +5. Compare output with existing Black/isort formatting |
| 127 | + |
| 128 | +### Performance Verification |
| 129 | + |
| 130 | +- Measure formatting speed improvement with Ruff vs Black+isort |
| 131 | +- Verify pre-commit hook execution time improvement |
| 132 | + |
| 133 | +## Implementation Considerations |
| 134 | + |
| 135 | +### Dependency Management |
| 136 | + |
| 137 | +- Ruff will be added to the "# Development & Debugging" section in pyproject.toml dependencies (following the existing organizational pattern where development tools like django-debug-toolbar are grouped) |
| 138 | +- Black and isort configurations will be removed from pyproject.toml (they are not explicitly listed as dependencies, only configured) |
| 139 | +- uv will handle Ruff installation and version management |
| 140 | +- Ruff will be placed appropriately within the development tools section to maintain logical grouping |
| 141 | + |
| 142 | +### Backward Compatibility |
| 143 | + |
| 144 | +- Ruff's Black-compatible formatter ensures existing code style is maintained |
| 145 | +- Django-aware import sorting preserves current import organization |
| 146 | +- Line length and exclusion patterns remain unchanged |
| 147 | + |
| 148 | +### Team Adoption |
| 149 | + |
| 150 | +- Developers will need to update their local pre-commit hooks |
| 151 | +- IDE integrations may need to be updated to use Ruff instead of Black |
| 152 | +- Documentation will guide developers through the transition |
0 commit comments