|
| 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 | +**Rationale**: Centralizing all tool configuration in pyproject.toml follows Python packaging standards and simplifies maintenance. |
| 32 | + |
| 33 | +- Remove `[tool.black]` section |
| 34 | +- Remove `[tool.isort]` section |
| 35 | +- Add comprehensive `[tool.ruff]` configuration |
| 36 | +- Add Ruff as a development dependency in the "# Development & Debugging" section alongside django-debug-toolbar |
| 37 | +- Remove Black and isort from dependencies if present |
| 38 | + |
| 39 | +#### Pre-commit Configuration Updates |
| 40 | + |
| 41 | +**Rationale**: Maintaining pre-commit integration ensures code quality checks remain automated and consistent across the development team. |
| 42 | + |
| 43 | +- Replace Black hook (currently using psf/black rev 23.3.0) with Ruff formatter hook |
| 44 | +- Replace isort hook (currently using pycqa/isort rev 5.12.0) with Ruff import sorting hook |
| 45 | +- Maintain existing exclusion patterns for migrations and .vscode folders |
| 46 | +- Keep all other pre-commit hooks unchanged (trailing-whitespace, end-of-file-fixer, etc.) |
| 47 | + |
| 48 | +#### Documentation Updates |
| 49 | + |
| 50 | +**Rationale**: Comprehensive documentation updates ensure all team members and new contributors understand the current tooling and maintain consistency across the project. |
| 51 | + |
| 52 | +- Update `.kiro/steering/tech.md` Code Quality Tools section to reference Ruff instead of Black and isort |
| 53 | +- Update `.kiro/steering/structure.md` Development Conventions section to reference Ruff formatting |
| 54 | +- Check and update `README.md` if it contains references to Black or isort |
| 55 | +- Update any developer setup instructions to include Ruff-specific commands |
| 56 | +- Ensure all documentation maintains consistency with Ruff usage |
| 57 | + |
| 58 | +### Ruff Configuration Sections |
| 59 | + |
| 60 | +Based on the current Black and isort configuration, Ruff will be configured as follows: |
| 61 | + |
| 62 | +#### Core Settings |
| 63 | + |
| 64 | +```toml |
| 65 | +[tool.ruff] |
| 66 | +target-version = "py311" |
| 67 | +exclude = [migrations, build artifacts, etc.] |
| 68 | +``` |
| 69 | + |
| 70 | +#### Import Sorting Settings |
| 71 | + |
| 72 | +**Rationale**: Django-aware import sorting maintains the existing project's import organization patterns while leveraging Ruff's performance benefits. This ensures proper separation of Django imports from other third-party libraries, maintaining the project's existing import organization standards. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +## Data Models |
| 77 | + |
| 78 | +No data models are affected by this migration as it only changes development tooling configuration. |
| 79 | + |
| 80 | +## Error Handling |
| 81 | + |
| 82 | +### Migration Validation |
| 83 | + |
| 84 | +- Verify Ruff produces equivalent formatting to Black on existing codebase |
| 85 | +- Ensure import sorting maintains Django-aware section organization |
| 86 | +- Test pre-commit hooks function correctly with new configuration |
| 87 | + |
| 88 | +### Rollback Strategy |
| 89 | + |
| 90 | +- Keep backup of original Black/isort configuration |
| 91 | +- Document steps to revert if issues are discovered |
| 92 | +- Maintain git history for easy rollback |
| 93 | + |
| 94 | +## Testing Strategy |
| 95 | + |
| 96 | +### Configuration Testing |
| 97 | + |
| 98 | +1. **Format Consistency Test**: Run Ruff formatter on existing codebase and verify minimal changes |
| 99 | +2. **Import Sorting Test**: Verify Ruff import sorting maintains Django section organization |
| 100 | +3. **Pre-commit Integration Test**: Test pre-commit hooks with Ruff configuration |
| 101 | +4. **Exclusion Pattern Test**: Verify migrations and other excluded files are not processed |
| 102 | + |
| 103 | +### Validation Steps |
| 104 | + |
| 105 | +**Rationale**: These validation steps ensure the migration maintains code quality and formatting consistency while verifying all requirements are met. |
| 106 | + |
| 107 | +1. Install Ruff and configure in pyproject.toml |
| 108 | +2. Run `docker compose run --rm app uv run ruff format --check .` on codebase to verify compatibility (respects pyproject.toml settings) |
| 109 | +3. Run `docker compose run --rm app uv run ruff check --select I .` to test import sorting (respects pyproject.toml settings) |
| 110 | +4. Test pre-commit hooks in containerized environment using `docker compose run --rm app pre-commit run --all-files` |
| 111 | +5. Compare output with existing Black/isort formatting to ensure consistency |
| 112 | +6. Verify uv commands work correctly with Ruff (addresses Requirement 4.4) |
| 113 | +7. Confirm migrations are properly excluded from formatting and linting |
| 114 | + |
| 115 | +### Performance Verification |
| 116 | + |
| 117 | +- Measure formatting speed improvement with Ruff vs Black+isort |
| 118 | +- Verify pre-commit hook execution time improvement |
| 119 | + |
| 120 | +## Implementation Considerations |
| 121 | + |
| 122 | +### Dependency Management |
| 123 | + |
| 124 | +**Rationale**: Proper dependency management ensures Ruff is available in all development environments and follows the project's existing organizational patterns. |
| 125 | + |
| 126 | +- Ruff will be added to the "# Development & Debugging" section in pyproject.toml dependencies (addresses Requirement 4.1, 4.3) |
| 127 | +- Black and isort configurations will be removed from pyproject.toml (addresses Requirement 4.2) |
| 128 | +- uv will handle Ruff installation and version management (addresses Requirement 4.4) |
| 129 | +- Ruff will be placed appropriately within the development tools section to maintain logical grouping |
| 130 | + |
| 131 | +### Backward Compatibility |
| 132 | + |
| 133 | +- Ruff's Black-compatible formatter ensures existing code style is maintained |
| 134 | +- Django-aware import sorting preserves current import organization |
| 135 | +- Line length and exclusion patterns remain unchanged |
| 136 | + |
| 137 | +### Team Adoption |
| 138 | + |
| 139 | +- Developers will need to update their local pre-commit hooks |
| 140 | +- IDE integrations may need to be updated to use Ruff instead of Black |
| 141 | +- Documentation will guide developers through the transition |
| 142 | + |
| 143 | +## Requirements Traceability |
| 144 | + |
| 145 | +This design addresses all requirements from the requirements document: |
| 146 | + |
| 147 | +**Requirement 1 (Tool Replacement)**: Addressed through pyproject.toml configuration sections that replace Black and isort with Ruff while maintaining migration exclusions, and Django-aware import sorting. |
| 148 | + |
| 149 | +**Requirement 2 (Pre-commit Integration)**: Addressed through pre-commit configuration updates that replace Black and isort hooks with Ruff equivalents while maintaining existing exclusion patterns. |
| 150 | + |
| 151 | +**Requirement 3 (Documentation Updates)**: Addressed through systematic updates to steering documents, README, and developer setup instructions to reflect Ruff usage consistently. |
| 152 | + |
| 153 | +**Requirement 4 (Dependency Management)**: Addressed through adding Ruff to development dependencies and removing Black/isort configurations, with uv handling installation and version management. |
0 commit comments