home / skills / basher83 / lunar-claude / python-code-quality

This skill helps you establish Python code quality with ruff and pyright, enabling linting, formatting, and type checks across projects.

npx playbooks add skill basher83/lunar-claude --skill python-code-quality

Review the files below or copy the command above to add this skill to your agents.

Files (13)
SKILL.md
3.6 KB
---
name: python-code-quality
description: >
  Python code quality tooling with ruff and pyright.
  Use when setting up linting, formatting, type checking,
  configuring ruff or pyright, or establishing code quality standards.
---

# Python Code Quality with Ruff and Pyright

Modern Python code quality tooling using ruff (linting + formatting) and pyright (type checking).

## Quick Start

### Install Tools

```bash
# Using uv (recommended)
uv add --dev ruff pyright

# Using pip
pip install ruff pyright
```

### Run Quality Checks

```bash
# Format and lint with ruff
ruff check --fix .
ruff format .

# Type check with pyright
pyright
```

## When to Use This Skill

Use this skill when:

- Setting up linting and formatting for a Python project
- Configuring type checking
- Establishing code quality standards for a team
- Integrating quality checks into pre-commit or CI/CD
- Migrating from black/flake8/mypy to ruff/pyright

## Ruff: All-in-One Linter and Formatter

Ruff combines the functionality of flake8, black, isort, and more:

**Benefits:**

- 10-100x faster than alternatives
- Drop-in replacement for black, flake8, isort
- Single tool configuration
- Auto-fix for many violations

**Configuration:** See `reference/ruff-configuration.md`

## Pyright: Fast Type Checker

Pyright provides static type checking for Python:

**Benefits:**

- Faster than mypy
- Better editor integration (VS Code, etc.)
- Incremental type checking
- Configurable strictness

**Configuration:** See `reference/pyright-configuration.md`

## Recommended Workflow

1. **Pre-commit Hooks** - Run quality checks before each commit
   - See: `patterns/pre-commit-integration.md`

2. **CI/CD Quality Gates** - Block merges on quality failures
   - See: `patterns/ci-cd-quality-gates.md`

3. **Editor Integration** - Real-time feedback while coding
   - See: `workflows/quality-workflow.md`

## Configuration Templates

Generic starter configs in `examples/`:

- `pyrightconfig-starter.json` - Minimal type checking
- `pyrightconfig-strict.json` - Strict type checking
- `ruff-minimal.toml` - Basic linting + formatting
- `ruff-comprehensive.toml` - Full-featured config

## Helper Tools

- `tools/python_formatter.py` - Batch format Python files
- `tools/python_ruff_checker.py` - Check code quality

## Ruff vs Alternatives

| Feature | Ruff | Black + Flake8 + isort |
|---------|------|------------------------|
| Speed | ⚡⚡⚡ | ⚡ |
| Configuration | Single file | Multiple files |
| Auto-fix | ✅ | Partial |
| Formatting | ✅ | Black only |
| Import sorting | ✅ | isort only |

## Pyright vs mypy

| Feature | Pyright | mypy |
|---------|---------|------|
| Speed | ⚡⚡⚡ | ⚡⚡ |
| VS Code integration | Native | Extension |
| Configuration | JSON | INI/TOML |
| Incremental checking | ✅ | ✅ |

## Common Patterns

### Ignore Specific Lines

```python
# Ruff
x = 1  # noqa: F841  # Unused variable

# Pyright
x = 1  # type: ignore
```

### Configure Per-Directory

```toml
# ruff.toml
[tool.ruff]
exclude = ["migrations/", "scripts/"]

[tool.ruff.lint]
select = ["E", "F", "W"]
```

## Next Steps

1. Choose config template from `examples/`
2. Set up pre-commit hooks: `patterns/pre-commit-integration.md`
3. Add CI/CD quality gates: `patterns/ci-cd-quality-gates.md`
4. Configure editor integration: `workflows/quality-workflow.md`

## Reference Documentation

- `reference/ruff-configuration.md` - Complete ruff configuration guide
- `reference/ruff-linting-settings.md` - Linting rule categories
- `reference/ruff-formatting-settings.md` - Formatting options
- `reference/pyright-configuration.md` - Pyright setup and configuration

Overview

This skill provides practical guidance and tooling for modern Python code quality using ruff for linting/formatting and pyright for static type checking. It helps teams and individual developers set up, configure, and integrate these tools into editors, pre-commit hooks, and CI/CD pipelines. The goal is faster checks, fewer tools to manage, and clearer type guarantees.

How this skill works

The skill outlines installing ruff and pyright, running ruff to lint and auto-fix formatting, and running pyright for incremental type checks. It includes starter configuration templates for both tools and patterns for per-directory overrides and ignore rules. It also recommends workflows for editor integration, pre-commit hooks, and CI quality gates to enforce standards automatically.

When to use it

  • Setting up linting and formatting for a new or existing Python project
  • Configuring or tightening static type checking with pyright
  • Standardizing code quality rules across a team or organization
  • Integrating quality checks into pre-commit workflows and CI/CD pipelines
  • Migrating from black/flake8/isort/mypy to a faster, unified toolchain

Best practices

  • Start with minimal configs and iterate toward stricter settings to avoid blocking development
  • Use ruff as a single source for linting, formatting, and import sorting to simplify config management
  • Run ruff --fix and ruff format as part of pre-commit hooks to keep commits clean
  • Adopt pyright incremental mode and a staged rollout for stricter type checks (e.g., per-package configs)
  • Enforce quality gates in CI to prevent regressions and keep the main branch healthy

Example use cases

  • Add ruff and pyright to dev dependencies and create starter config files for the repo
  • Implement pre-commit hooks that run ruff --fix and pyright --outputjson to catch issues before commits
  • Create CI jobs that run ruff check and pyright and fail the pipeline on violations
  • Migrate from black/flake8/isort/mypy by mapping rules into ruff and adopting pyright configs incrementally
  • Configure per-directory ruff excludes for generated code or migrations to avoid noisy warnings

FAQ

Can ruff replace black, flake8, and isort?

Yes. Ruff aims to be a drop-in replacement by providing formatting, linting, and import sorting with faster performance and many compatible rules.

Is pyright faster than mypy and suitable for CI?

Yes. Pyright is typically faster, supports incremental checks, and integrates well with editors and CI. Use strict configs gradually to surface type issues without overwhelming the team.