Expert guidance for writing professional Python code following industry best practices including PEP 8 compliance, testing, type hints, error handling, and modern tooling. Use this skill when writing new Python code, refactoring existing code, setting up Python projects, implementing tests, or ensuring code quality and maintainability. Emphasizes: PEP 8, modularity, DRY principle, TDD, virtual environments (venv), and modern tooling (Ruff, Black, Mypy).
A comprehensive Claude Code skill for writing professional, maintainable Python code following industry best practices.
This skill provides expert guidance on:
The skill is automatically invoked when you:
Ruff: Fast, Rust-based linter and formatter
ruff check --fix . # Lint and auto-fix
ruff format . # Format code
Black: Opinionated code formatter
black . # Format all files
Mypy: Static type checker
mypy src/ # Check types
uv: Modern, fast package manager
uv venv # Create virtual environment
uv pip install pkg # Install packages
python-best-practices/
├── SKILL.md # Main skill instructions for Claude
├── QUICK_REFERENCE.md # Quick lookup guide
├── README.md # This file
└── examples/
├── project_structure.py # Well-structured module
├── testing_examples.py # Test examples
├── type_hints_demo.py # Type hints showcase
└── error_handling.py # Exception handling
# Using skillz CLI
skillz install python-best-practices
# Or manually
mkdir -p ~/.claude/skills/python-best-practices
cp -r . ~/.claude/skills/python-best-practices/
# Using skillz CLI
skillz install python-best-practices --target project
# Or manually
mkdir -p .claude/skills/python-best-practices
cp -r . .claude/skills/python-best-practices/
mkdir my_project
cd my_project
uv venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
[project]
name = "my-project"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = []
[project.optional-dependencies]
dev = ["pytest", "ruff", "mypy"]
[tool.ruff]
line-length = 99
select = ["E", "F", "I", "N", "W", "B"]
[tool.mypy]
strict = true
uv pip install --dev pytest ruff mypy
mkdir -p src/my_project tests
touch src/my_project/__init__.py
touch tests/__init__.py
ruff check --fix .ruff format .mypy src/pytestpytest --cov=srcfrom pathlib import Path
def process_data_file(
input_path: Path,
output_path: Path,
*,
validate: bool = True,
encoding: str = "utf-8"
) -> dict[str, int]:
"""Process data file and generate statistics.
Args:
input_path: Path to input data file
output_path: Path for processed output
validate: Whether to validate data before processing
encoding: File encoding (default: utf-8)
Returns:
Dictionary with processing statistics:
- 'lines_processed': Number of lines processed
- 'errors': Number of errors encountered
Raises:
FileNotFoundError: If input_path doesn't exist
ValueError: If validation fails
Example:
>>> stats = process_data_file(
... Path("data.txt"),
... Path("output.txt"),
... validate=True
... )
>>> print(stats['lines_processed'])
100
"""
if not input_path.exists():
raise FileNotFoundError(f"Input file not found: {input_path}")
# Implementation...
return {'lines_processed': 100, 'errors': 0}
# 1. Write the test first
def test_parse_config():
"""Test configuration parsing."""
config = parse_config({"timeout": "30", "debug": "true"})
assert config.timeout == 30
assert config.debug is True
# 2. Implement minimal code to pass
from dataclasses import dataclass
@dataclass
class Config:
timeout: int
debug: bool
def parse_config(data: dict) -> Config:
return Config(
timeout=int(data['timeout']),
debug=data['debug'].lower() == 'true'
)
# 3. Refactor while keeping tests green
See QUICK_REFERENCE.md for a complete pyproject.toml template with all tool configurations.
Install pre-commit hooks to automatically check code quality:
uv pip install pre-commit
pre-commit install
Create .pre-commit-config.yaml:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
When Claude Code uses this skill, it will:
When reviewing code, this skill ensures:
To improve this skill:
This skill is part of the skillz repository and follows the same license.
For issues or suggestions:
Based on:
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer