| name | improving-python-code-quality |
| description | Improves Python library code quality through ruff linting, mypy type checking, Pythonic idioms, and refactoring. Use when reviewing code for quality issues, adding type hints, configuring static analysis tools, or refactoring Python library code. |
Python Code Quality
Quick Reference
| Tool | Purpose | Command |
|---|
| ruff | Lint + format | ruff check src && ruff format src |
| mypy | Type check | mypy src |
Ruff Configuration
Minimal config in pyproject.toml:
[tool.ruff]
line-length = 88
target-version = "py310"
[tool.ruff.lint]
select = ["E", "W", "F", "I", "B", "C4", "UP"]
For full configuration options, see RUFF_CONFIG.md.
MyPy Configuration
[tool.mypy]
python_version = "3.10"
disallow_untyped_defs = true
warn_return_any = true
For strict settings and overrides, see MYPY_CONFIG.md.
Type Hints Patterns
def process(items: list[str]) -> dict[str, int]: ...
def fetch(url: str, timeout: int | None = None) -> bytes: ...
def apply(func: Callable[[int], str], value: int) -> str: ...
T = TypeVar("T")
def first(items: Sequence[T]) -> T | None: ...
For protocols and advanced patterns, see TYPE_PATTERNS.md.
Common Anti-Patterns
def process(items: list = []):
...
def process(items: list | None = None):
items = items or []
...
try:
...
except:
pass
try:
...
except ValueError as e:
logger.error(e)
Pythonic Idioms
for item in items:
for i, item in enumerate(items):
value = d.get(key, default)
with open(path) as f:
squares = [x**2 for x in numbers]
Module Organization
src/my_library/
├── __init__.py # Public API exports
├── _internal.py # Private (underscore prefix)
├── exceptions.py # Custom exceptions
├── types.py # Type definitions
└── py.typed # Type hint marker
Checklist
Code Quality:
- [ ] ruff check passes
- [ ] mypy passes (strict mode)
- [ ] Public API has type hints
- [ ] Public API has docstrings
- [ ] No mutable default arguments
- [ ] Specific exception handling
- [ ] py.typed marker present
Learn More
This skill is based on the Code Quality section of the Guide to Developing High-Quality Python Libraries by Will McGinnis.