ml-format
Scannednpx machina-cli add skill nishide-dev/claude-code-ml-research/ml-format --openclawCode Formatting
Format Python code with ruff formatter and optionally fix auto-fixable linting issues.
Process
1. Determine Scope
Ask the user what to format:
- All files: Format entire project (default)
- Specific directory: Format only specified directory (e.g.,
src/,tests/) - Specific files: Format only specified files
- Changed files only: Format only files modified in git working directory
If not specified, format all Python files in the project.
2. Run Ruff Format
Format code with ruff:
# Format all files
uv run ruff format .
# Format specific directory
uv run ruff format src/
# Format specific files
uv run ruff format src/train.py src/models/model.py
# Preview changes without applying (dry-run)
uv run ruff format --check --diff .
Output interpretation:
- Shows files being formatted
- "X files reformatted, Y files left unchanged"
- Exit code 0 → Success
3. Run Ruff Check with Auto-fix (Optional)
After formatting, optionally fix auto-fixable linting issues:
# Fix all auto-fixable issues
uv run ruff check --fix .
# Fix specific directory
uv run ruff check --fix src/
# Fix specific rules only
uv run ruff check --fix --select F,I . # Fix imports and undefined names
Output interpretation:
- Shows fixed violations
- Lists remaining violations that need manual fixes
- "Fixed X violations, Y remaining"
4. Verify Results
After formatting and fixing, verify the results:
# Check formatting is correct
uv run ruff format --check .
# Check remaining lint issues
uv run ruff check .
Report any remaining issues that need manual attention.
5. Git Status Check
If working in a git repository, show what changed:
# Show modified files
git status --short
# Show diff of changes (optional, if user wants to review)
git diff
Inform user about changes so they can review before committing.
Options
Check-only Mode
To preview changes without applying:
# Show what would be formatted
uv run ruff format --check .
# Show detailed diff
uv run ruff format --check --diff .
Use this when user wants to see changes before applying.
Selective Fixing
To fix only specific types of issues:
# Fix only import order
uv run ruff check --fix --select I .
# Fix only unused imports
uv run ruff check --fix --select F401 .
# Fix only whitespace issues
uv run ruff check --fix --select W .
Aggressive Formatting
For more aggressive formatting (use with caution):
# Format with maximum line length
uv run ruff format --line-length 120 .
# Note: This overrides project config, confirm with user first
Git Integration
Format Changed Files Only
# Get list of changed Python files
changed_files=$(git diff --name-only --diff-filter=ACMR "*.py")
if [ -n "$changed_files" ]; then
# Format only changed files
echo "$changed_files" | xargs uv run ruff format
echo "$changed_files" | xargs uv run ruff check --fix
else
echo "No Python files changed"
fi
Pre-commit Integration
Suggest setting up pre-commit hooks:
# Install pre-commit (if using)
uv run pre-commit install
# Run hooks manually
uv run pre-commit run --all-files
Error Handling
If commands fail:
-
ruff not found:
- Suggest:
uv add --dev ruff - Or:
pixi add ruff
- Suggest:
-
Syntax errors in code:
- ruff format may fail on invalid Python syntax
- Report the syntax error location
- Ask user to fix syntax errors first
-
Permission errors:
- Check file permissions
- Suggest running with appropriate permissions
-
uv not found:
- Suggest installing uv
- Or use alternative:
python -m ruff format .
Best Practices
- Format before fixing: Run formatter first, then linter
- Review changes: Check git diff before committing
- Use in CI: Add formatting checks to CI pipeline
- Consistent style: Let ruff handle all formatting decisions
- Incremental adoption: Format one directory at a time for large projects
Output Example
Formatting Python files with ruff...
✓ Formatted 15 files, 3 files left unchanged
Fixing auto-fixable lint issues...
✓ Fixed 8 violations
⚠ 2 violations remaining (need manual fix):
- src/train.py:45:1 F841 Local variable 'x' is assigned but never used
- src/model.py:12:5 E501 Line too long (95 > 88 characters)
Summary:
✓ Code formatted successfully
✓ 8 issues auto-fixed
⚠ 2 issues need manual attention
To review changes: git diff
To commit changes: git add -A && git commit -m "Format code with ruff"
Related Commands
ml-lint: Run code quality checks without modifying filesml-validate: Comprehensive project validation including code quality
Source
git clone https://github.com/nishide-dev/claude-code-ml-research/blob/main/skills/ml-format/SKILL.mdView on GitHub Overview
ml-format standardizes Python code by using Ruff's formatter to reformat and style code. It can format the entire project, a specific directory, or particular files, and can optionally fix auto-fixable lint issues. This helps maintain a consistent code style across the project and speeds up preparing code for commits.
How This Skill Works
First, determine the scope of formatting (all files, a directory, specific files, or changed files). Then run Ruff format to apply style changes (e.g., uv run ruff format . or uv run ruff format src/), with a dry-run option (--check --diff) to preview. Optionally run Ruff check --fix to address auto-fixable issues, then verify results with ruff format --check and ruff check, and review the git status to confirm changes.
When to Use It
- Before committing or merging, format all Python files to ensure a consistent style.
- Format a specific directory (e.g., src/ or tests/) when preparing a feature or patch.
- Format only the files you touched to minimize churn.
- Format changed files only in the git working directory to align recent edits.
- Preview changes with a dry-run (--check) to review diffs before applying.
Quick Start
- Step 1: Decide the scope of formatting (default: all Python files).
- Step 2: Run formatting, e.g., uv run ruff format . or uv run ruff format src/.
- Step 3: Optional: run uv run ruff check --fix . to auto-fix issues, then verify with uv run ruff format --check . and uv run ruff check .
Best Practices
- Run a dry-run first to preview changes before applying.
- Format before committing to keep a clean diff.
- Limit scope to avoid broad, unnecessary edits.
- After formatting, run ruff check --fix to auto-fix issues, then re-run format check.
- Review the diff or git status to understand changes before committing.
Example Use Cases
- Format the entire project before a release to ensure consistent style.
- Format only the src/ directory before merging a feature branch.
- Format only specific files like train.py and models/model.py after a refactor.
- Use --check --diff to preview changes in CI before applying formatting.
- Format changed files automatically in a git hook during pre-commit.