Repository navigation
05. Code Formatting
This guide covers the comprehensive code formatting and linting setup for TritonParse, ensuring consistent code quality across all contributions.
| Tool | Purpose | Version | Configuration |
|---|---|---|---|
| ufmt | Unified formatting driver | 2.9.0 | pyproject.toml |
| ruff-api | Code formatting | 0.2.0 | pyproject.toml |
| usort | Import sorting | 1.1.0 | pyproject.toml |
| Ruff | Linting and safe fixes | 0.16.0 | pyproject.toml |
graph LR
A[Source Code] --> B[ufmt]
B --> C[usort]
C --> D[ruff-api formatter]
D --> E[ruff check]
E --> F[Formatted and linted code]
C -.->|Import sorting| C1[Alphabetical imports]
D -.->|Formatting| D1[Code structure]
E -.->|Linting| E1[Style violations]
Why this order?
- ufmt - Runs usort, then the ruff-api formatter
- ruff - Checks lint rules separately and applies safe fixes when requested
# Install all development dependencies
make install-dev# Install tritonparse and its pinned development tools
pip install -e ".[dev]"# Fix all formatting issues
make format
# Fix safe lint issues
make lint
# Check formatting without fixing
make format-check
# Check all configured lint rules without fixing
make lint-check| Tool | Key Settings | Purpose |
|---|---|---|
| ruff-api | Line length: 88 | PEP 8 compliant formatting |
| usort | First-party detection: off | Alphabetical import sorting |
| Ruff | Explicit E4, E7, E9, and F rules |
Stable, fast linting |
π Full Configuration Files
pyproject.toml:
[tool.ufmt]
formatter = "ruff-api"
sorter = "usort"
[tool.ruff]
line-length = 88
target-version = "py310"
[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]
[tool.usort]
first_party_detection = falseMakefile:
# Formatting commands
format:
python -m tritonparse.tools.format_fix --format-only --verbose
format-check:
python -m tritonparse.tools.format_fix --format-only --check-only --verbose
# Linting commands
lint:
python -m tritonparse.tools.format_fix --lint-only --verbose
lint-check:
python -m tritonparse.tools.format_fix --lint-only --check-only --verbose# 1. Make your code changes
# 2. Run formatting
make format
# 3. Verify formatting and lint are clean
make format-check
make lint-check
# 4. Commit your changes
git add .
git commit -m "Your changes"Format check fails?
make format # Fix formatting automatically
make format-check # Verify formatting
make lint # Fix safe lint issues automatically
make lint-check # Verify lintNeed to check specific changes?
ufmt check path/to/file.py
ruff check path/to/file.pyFix individual files:
ufmt format path/to/file.py
ruff check path/to/file.py --fixClear tool caches:
rm -rf .ruff_cache __pycache__
make formatπ‘ Tip: Run
make format,make lint,make format-check, andmake lint-checkbefore committing.
The CI pipeline includes a format-check job that:
-
Installs dependencies via
make install-dev -
Checks formatting via
make format-check -
Verifies linting via
make lint-check - Blocks merging if formatting fails
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Set up Python 3.14
uses: actions/setup-python@v6
with:
python-version: "3.14"
- name: Install development dependencies
run: make install-dev
- name: Check code formatting
run: make format-check
- name: Check linting
run: make lint-checkCI Process:
- Install dependencies β
make install-dev - Check formatting β
make format-check - Verify linting β
make lint-check - Block merge if any check fails
ufmt and ruff-api - Run import sorting and deterministic code formatting as one atomic operation. The ruff-api formatter uses an 88-character line length.
usort - Sorts imports alphabetically within categories: standard library, third-party packages, and local modules. Groups imports with blank lines between categories.
Ruff - Fast linter that checks code style violations, unused imports, syntax errors, and type annotation issues. Auto-fixes safe issues.
# Before formatting
import triton
import os
import torch
from pathlib import Path
def my_function(x,y,z):
return x+y+z
# After: ufmt (usort β ruff-api formatter)
import os
from pathlib import Path
import torch
import triton
def my_function(x, y, z):
return x + y + z| Issue | Error Message | Quick Fix |
|---|---|---|
| Import Order | E402: Module level import not at top of file |
Move all imports to file top, before code |
| Formatting vs linting | Different checks report different issues | Run make format, then make lint
|
| Import Sorting | usort breaks functionality | Use # usort: skip comment for file/import |
| Line Length | Line exceeds 88 characters | Use parentheses for multi-line breaks |
Import Order:
# Wrong
print("Starting")
import os
# Correct
import os
print("Starting")Line Length:
# Use parentheses
result = some_function(
very_long_argument,
another_argument,
third_argument
)
# Split strings
message = (
"Long message that would "
"exceed line length"
)Skip Sorting:
# usort: skip
import special_module # usort: skip-
Run
make formatbefore every commit -
Use
make lint-checkto verify CI compatibility - Follow the tool chain order: ufmt (usort β ruff-api), then Ruff lint
- Keep line length at 88 characters
- Use descriptive commit messages for formatting changes
- Don't bypass formatting checks in CI
- Don't mix formatting with feature changes in commits
- Don't manually format code that tools can handle
- Don't ignore linting errors without good reason
- Don't modify tool configurations without discussion
VS Code: Configure ufmt as the formatter and install the Ruff extension for linting. Enable editor.formatOnSave in settings.
PyCharm: Configure ufmt as an external formatter and enable Ruff inspection.
Vim/Neovim: Configure your formatter integration to run ufmt and use Ruff's language server for linting.
π‘ See your editor's plugin documentation for detailed setup instructions.
This formatting setup follows PyTorch ecosystem patterns:
- ruff-api for primary formatting
- usort for import management (Meta/Facebook standard)
- Ruff for fast linting (modern Python tooling)
- 88-character line length
For formatting issues:
- Check this guide first
- Run
make format,make lint,make format-check, andmake lint-check - Review the troubleshooting section
- Check CI logs for specific errors
- Ask in GitHub Discussions
- Developer Guide - Complete development workflow
- Contributing - Contribution guidelines
- Python API Reference - API documentation
- Installation - Development setup
- GitHub Issues - Report formatting bugs
Note: This formatting setup ensures consistent code quality across all contributions. When in doubt, run make format, make lint, make format-check, and make lint-check.