Skip to content

Commit d386aba

Browse files
karbassiCopilot
andauthored
1510 replace black with ruff (#1512)
* spec * Configure Ruff to replace Black and isort - Remove [tool.black] and [tool.isort] configuration sections - Add ruff>=0.8.0 as development dependency - Configure [tool.ruff] with line-length=79, target-version=py311 - Set up [tool.ruff.format] with Black-compatible settings - Configure [tool.ruff.isort] with Django-aware import sorting - Add [tool.ruff.lint] with basic linting rules (E, F, W, I) - Exclude migrations directory from formatting and linting This maintains existing code style while consolidating formatting, import sorting, and linting into a single faster tool. * Replace Black and isort with Ruff for code formatting and linting - Update pre-commit configuration to use Ruff instead of Black/isort - Add Ruff as development dependency in pyproject.toml - Configure Ruff with Django-aware import sorting and formatting rules - Apply Ruff formatting across entire codebase: - Fix import ordering and grouping - Remove unused imports and variables - Fix string formatting (remove f-string prefixes where unnecessary) - Improve code style consistency - Fix minor linting issues (unused variables, comparison style) - Update task completion status in migration spec - Maintain existing line length and formatting preferences This migration consolidates two tools (Black + isort) into one (Ruff) while maintaining code quality standards and improving development workflow efficiency. * docs: Replace Black and isort references with Ruff in steering docs - Update tech.md to reference Ruff as combined linter and formatter - Update structure.md to use Ruff for formatting and import sorting - Maintain 79-character line length configuration - Preserve Django-aware import sorting documentation * Reset Ruff configuration to recommended defaults - Remove custom line-length setting (79 -> 88 chars default) - Remove custom exclude patterns, using Ruff's built-in defaults - Remove custom lint rule selections, using recommended rule set - Remove custom isort and format configurations - Keep Django-specific __init__.py wildcard import allowance - Update documentation to reflect simplified configuration * . * test: validate Ruff configuration and pre-commit integration - Test formatting compatibility with existing codebase - Verify import sorting functionality and Django-aware organization - Validate pre-commit hooks execute correctly in containerized environment - Confirm migration files are properly excluded from formatting - Fix 31 import sorting issues and reformat 25 files during testing - All Ruff functionality working as expected with proper exclusion patterns * fix: update Ruff configuration to resolve deprecation warnings - Move linter settings to [tool.ruff.lint] section - Remove COM812 rule to avoid conflicts with formatter - Add COM812 to ignore list to prevent formatter conflicts - Maintain all existing rule selections and configurations Resolves warnings about deprecated top-level linter settings and formatter conflicts with COM812 rule. * Update coderdojochi/models/session.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update coderdojochi/util.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update coderdojochi/models/session.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update coderdojochi/notifications.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update coderdojochi/models/session.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update weallcode/views/programs.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix save command * fix save command * Now the logic is correct and handles both cases: - None values: Sets them to 0 - Negative values: Sets them to 0 - Positive values: Leaves them unchanged This preserves the original intent of the code while being more readable than the redundant max() calls. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b1d243c commit d386aba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1187
-858
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Requirements Document
2+
3+
## Introduction
4+
5+
This feature involves migrating the We All Code Django project from using Black (code formatter) and isort (import sorter) to Ruff, which is a faster, all-in-one Python linter and formatter that can replace both tools. Ruff provides the same formatting capabilities as Black while also offering linting and import sorting functionality in a single, faster tool.
6+
7+
## Requirements
8+
9+
### Requirement 1: Tool Replacement
10+
11+
**User Story:** As a developer, I want to use Ruff instead of Black and isort, so that I have faster code formatting and linting with a single tool.
12+
13+
#### Acceptance Criteria
14+
15+
1. WHEN the project is configured THEN Ruff SHALL replace Black as the code formatter
16+
2. WHEN the project is configured THEN Ruff SHALL replace isort for import sorting
17+
3. WHEN Ruff is configured THEN it SHALL exclude migrations from formatting (same as current Black config)
18+
4. WHEN Ruff is configured THEN it SHALL maintain Django-aware import sorting sections with proper separation of Django imports from other third-party libraries
19+
20+
### Requirement 2: Pre-commit Integration
21+
22+
**User Story:** As a developer, I want the pre-commit hooks updated to use Ruff, so that code quality checks run automatically before commits.
23+
24+
#### Acceptance Criteria
25+
26+
1. WHEN pre-commit hooks are updated THEN they SHALL use Ruff instead of Black and isort
27+
2. WHEN pre-commit runs THEN it SHALL format code using Ruff
28+
3. WHEN pre-commit runs THEN it SHALL sort imports using Ruff
29+
4. WHEN pre-commit runs THEN it SHALL maintain the same exclusion patterns as before
30+
31+
### Requirement 3: Documentation Updates
32+
33+
**User Story:** As a developer, I want all project documentation updated to reflect the Ruff migration, so that new contributors understand the current tooling and existing developers have accurate reference materials.
34+
35+
#### Acceptance Criteria
36+
37+
1. WHEN documentation is updated THEN .kiro/steering/tech.md SHALL reference Ruff instead of Black and isort in the Code Quality Tools section
38+
2. WHEN documentation is updated THEN .kiro/steering/structure.md SHALL reference Ruff formatting conventions instead of Black
39+
3. WHEN documentation is updated THEN README.md SHALL be updated if it contains references to Black or isort
40+
4. WHEN documentation is updated THEN any developer setup instructions SHALL include Ruff-specific commands
41+
5. WHEN documentation is updated THEN all references to code formatting tools SHALL be consistent with Ruff usage
42+
43+
### Requirement 4: Dependency Management
44+
45+
**User Story:** As a developer, I want Ruff to be added as a project dependency, so that it's available in the development environment.
46+
47+
#### Acceptance Criteria
48+
49+
1. WHEN dependencies are updated THEN Ruff SHALL be added to pyproject.toml
50+
2. WHEN dependencies are updated THEN Black and isort SHALL be removed from dependencies (if present)
51+
3. WHEN Ruff is added THEN it SHALL be in the appropriate dependency group for development tools
52+
4. WHEN the configuration is complete THEN Ruff SHALL be usable via uv commands
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Implementation Plan
2+
3+
- [x] 1. Configure Ruff in pyproject.toml
4+
5+
- Remove existing [tool.black] and [tool.isort] configuration sections
6+
- Add comprehensive [tool.ruff] configuration with general settings (target-version = "py311", exclude migrations)
7+
- Add Ruff as a development dependency in the "# Development & Debugging" section alongside django-debug-toolbar
8+
- Add [tool.ruff.lint] section with comprehensive linting rules
9+
- _Requirements: 1.1, 1.2, 1.3, 1.4, 1.5, 4.1, 4.2, 4.3, 4.4_
10+
11+
- [x] 2. Update pre-commit configuration
12+
13+
- Replace Black hook (psf/black) with Ruff formatter hook (charliermarsh/ruff-pre-commit)
14+
- Replace isort hook (pycqa/isort) with Ruff import sorting hook using ruff-check
15+
- Maintain existing exclusion patterns for migrations and .vscode folders
16+
- Keep all other pre-commit hooks unchanged (trailing-whitespace, end-of-file-fixer, etc.)
17+
- _Requirements: 2.1, 2.2, 2.3, 2.4_
18+
19+
- [x] 3. Update documentation files
20+
- [x] 3.1 Update .kiro/steering/tech.md
21+
22+
- Replace Black and isort references with Ruff in Code Quality Tools section
23+
- Update tool descriptions to reflect Ruff's combined formatting and linting capabilities
24+
- _Requirements: 3.1, 3.6_
25+
26+
- [x] 3.2 Update .kiro/steering/structure.md
27+
28+
- Replace Black formatter references with Ruff in Development Conventions section
29+
- Update code style documentation to reference Ruff instead of Black and isort
30+
- Maintain Django-aware import sorting documentation
31+
- _Requirements: 3.2, 3.6_
32+
33+
- [x] 3.3 Check and update README.md if needed
34+
35+
- Search for any references to Black or isort in README.md (none found)
36+
- Update developer setup instructions to include Ruff-specific commands if present (none needed)
37+
- Ensure consistency with Ruff usage throughout documentation
38+
- _Requirements: 3.3, 3.4, 3.5_
39+
40+
- [-] 4. Enhance Ruff configuration for complete migration
41+
- [ ] 4.1 Complete pyproject.toml Ruff configuration
42+
43+
- Add exclude patterns for migrations and other directories
44+
- Add [tool.ruff.isort] section with Django-aware import sorting (known-django, section-order, combine-as-imports)
45+
- _Requirements: 1.1, 1.2, 1.3, 1.4, 1.5_
46+
47+
- [x] 5. Test Ruff configuration
48+
- [x] 5.1 Validate formatting compatibility
49+
50+
- Run `docker compose run --rm app uv run ruff format --check .` on existing codebase to verify minimal changes
51+
- Compare Ruff output with current formatting to ensure consistency
52+
- Verify migrations are excluded from formatting (configured in pyproject.toml)
53+
- _Requirements: 1.1, 1.3, 1.4_
54+
55+
- [x] 5.2 Validate import sorting
56+
57+
- Run `docker compose run --rm app uv run ruff check --select I .` to test import sorting functionality
58+
- Verify Django-aware section organization is maintained
59+
- Test that import sorting follows isort-compatible behavior using `docker compose run --rm app uv run ruff check --select I --fix .`
60+
- _Requirements: 1.2, 1.5_
61+
62+
- [x] 5.3 Test pre-commit integration
63+
- Run `docker compose run --rm app pre-commit run --all-files` to test pre-commit hooks in containerized environment
64+
- Test that Ruff hooks execute correctly and maintain exclusion patterns
65+
- Verify pre-commit performance improvement with Ruff using `docker compose run --rm app pre-commit run ruff-format ruff-check`
66+
- _Requirements: 2.1, 2.2, 2.3, 2.4_

.kiro/steering/structure.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Extends django-allauth for custom authentication:
9191

9292
### Code Style
9393

94-
- Black formatter with 79 character line length
95-
- isort for import organization with Django-aware sections
94+
- Ruff formatter and linter with 79 character line length
95+
- Ruff import sorting with Django-aware sections
9696
- Migrations excluded from formatting
9797

9898
### Database

.kiro/steering/tech.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
## Code Quality Tools
2727

28-
- **Black** - Code formatter (line length: 79)
29-
- **isort** - Import sorting with Black profile
28+
- **Ruff** - Fast Python linter and formatter with import sorting
3029
- **django-nose** - Test runner
3130

3231
## Common Commands

.pre-commit-config.yaml

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
1+
exclude: "^docs/|/migrations/|devcontainer.json"
2+
default_stages: [pre-commit]
3+
minimum_pre_commit_version: "3.2.0"
4+
15
default_language_version:
26
python: python3.11
37

4-
# Ignore all 'migration' folders and .vscode folder
5-
exclude: '^(\.vscode\/?)|(.*\/migrations\/.*)$'
6-
78
repos:
89
# pre-commit hooks
910
- repo: https://github.com/pre-commit/pre-commit-hooks
10-
rev: v3.2.0
11+
rev: v5.0.0
1112
hooks:
12-
- id: no-commit-to-branch
13-
args: [--branch, main]
1413
- id: trailing-whitespace
1514
- id: end-of-file-fixer
16-
- id: check-yaml
1715
- id: check-json
1816
- id: check-toml
17+
- id: check-xml
18+
- id: check-yaml
19+
- id: debug-statements
20+
- id: check-builtin-literals
21+
- id: check-case-conflict
22+
- id: check-docstring-first
23+
- id: detect-private-key
24+
- id: no-commit-to-branch
25+
args: [--branch, main]
1926
- id: check-added-large-files
2027
- id: check-merge-conflict
21-
- id: detect-private-key
2228
- id: mixed-line-ending
2329
args: [--fix=lf]
2430

25-
# isort
26-
- repo: https://github.com/pycqa/isort
27-
rev: 5.12.0
28-
hooks:
29-
- id: isort
30-
31-
# black
32-
- repo: https://github.com/psf/black
33-
rev: 23.3.0
31+
# Run the Ruff linter.
32+
- repo: https://github.com/astral-sh/ruff-pre-commit
33+
rev: v0.12.5
3434
hooks:
35-
- id: black
36-
args: [--preview]
35+
# Linter
36+
- id: ruff-check
37+
args: [--fix, --exit-non-zero-on-fix]
38+
# Formatter
39+
- id: ruff-format

accounts/urls.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from django.conf.urls import include
22
from django.urls import path
33

4-
from .views import (
5-
AccountHomeView,
6-
LoginView,
7-
SignupView,
8-
)
4+
from .views import AccountHomeView
5+
from .views import LoginView
6+
from .views import SignupView
97

108
urlpatterns = [
119
path("", AccountHomeView.as_view(), name="account_home"),

0 commit comments

Comments
 (0)