DEV Community

cycy
cycy

Posted on

Elevating Your Python Code Quality: Setting Up Ruff in Your Workflow

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up VS Code with Ruff Extension

  1. Open VS Code
  2. Install the "Ruff" extension:

    • Press Cmd+Shift+X to open Extensions
    • Search for "Ruff"
    • Install the extension by Astral
  3. Configure VS Code settings:

    • Press Cmd+, to open Settings
    • Click on the "Open Settings (JSON)" icon in the top right
    • Add these settings:
{
  "editor.formatOnSave": true,
  "ruff.organizeImports": true,
  "ruff.fixAll": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.ruff": true
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Important Tips

  1. Check Only Your Changes: The script focuses only on files you've changed
  2. Local Configuration: None of this will be pushed to the repo
  3. VS Code Integration: Ruff will help you write clean code as you type
  4. 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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay