Have you ever submitted code only to have reviewers point out style issues, unused imports, or inconsistent formatting? As Python developers, we've all been there. In this post, I'll share how I transformed my development workflow by integrating Ruff—a blazingly fast Python linter and formatter—into my local environment.
Unlike traditional approaches that require team-wide implementation, I'll show you how to set up these powerful code quality tools just for your feature branches without affecting the main codebase. Whether you're working on an open-source project or a team with strict merge policies, these techniques will help you write cleaner, more professional Python code that follows PEP standards automatically. Say goodbye to style-related code review comments and hello to a more efficient coding experience!
Step 1: Install Ruff in Your Conda Environment
# Activate your conda environment
conda activate my-venv
# Install Ruff and pre-commit through conda
conda install -c conda-forge ruff pre-commit
# If you need the latest versions, you can use pip instead:
# pip install --upgrade ruff pre-commit
Step 2: Set Up Local-Only Configuration
# Navigate to your project
cd demo-project
# Create a local pyproject.toml for Ruff
cat > pyproject.toml << EOF
[tool.ruff]
line-length = 88
target-version = "py311" # Match your Python version
[tool.ruff.lint]
# E: pycodestyle errors
# F: pyflakes
# I: isort
# B: flake8-bugbear
select = ["E", "F", "I", "B"]
ignore = []
fixable = ["ALL"]
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
line-ending = "auto"
EOF
# Make sure this file stays local to your machine
echo "pyproject.toml" >> .git/info/exclude
Step 3: Set Up VS Code with Ruff Extension
- Open VS Code
-
Install the "Ruff" extension:
- Press
Cmd+Shift+X
to open Extensions - Search for "Ruff"
- Install the extension by Astral
- Press
-
Configure VS Code settings:
- Press
Cmd+,
to open Settings - Click on the "Open Settings (JSON)" icon in the top right
- Add these settings:
- Press
{
"editor.formatOnSave": true,
"ruff.organizeImports": true,
"ruff.fixAll": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": true
}
}
Step 4: Create a Script to Lint Only Your Changes
# Create a script to check only your changes
cat > check_my_changes.sh << EOF
#!/bin/bash
# Make sure we're in the project directory
cd "\$(git rev-parse --show-toplevel)"
echo "Getting changed Python files compared to dev branch..."
# Get only Python files that YOU changed compared to dev
MODIFIED_FILES=\$(git diff --name-only origin/dev...HEAD | grep '\.py$')
if [ -z "\$MODIFIED_FILES" ]; then
echo "No Python files changed in your branch."
exit 0
fi
echo "============================================="
echo "Checking these files:"
echo "\$MODIFIED_FILES"
echo "============================================="
# Run Ruff with fixes on your changed files
echo "\$MODIFIED_FILES" | xargs ruff check --fix
# Show results
echo "============================================="
echo "Done! Your code is now cleaner."
echo "============================================="
EOF
# Make it executable
chmod +x check_my_changes.sh
# Add to git exclude (keeps it local to your machine)
echo "check_my_changes.sh" >> .git/info/exclude
Step 5: Workflow for Feature Branches
# Always start from latest dev
git checkout dev
git pull origin dev
# Create your feature branch
git checkout -b feature/your-feature-name
# Make your code changes...
# Before committing, clean up just your changes
./check_my_changes.sh
# Review the fixes and make sure everything looks good
# Commit your clean code
git add .
git commit -m "Your commit message"
# Push to remote
git push origin feature/your-feature-name
Important Tips
- Check Only Your Changes: The script focuses only on files you've changed
- Local Configuration: None of this will be pushed to the repo
- VS Code Integration: Ruff will help you write clean code as you type
- Best of Both Worlds: You get clean code without changing the project setup
Now you can write code that meets PEP standards and keeps your PRs clean, even though the project doesn't officially use these tools yet!
Top comments (0)