---
description: Python Tooling
alwaysApply: false
---

# Python Tooling

Modern Python tooling is fast, integrated, and opinionated.

## pyproject.toml

```toml
[project]
name = "mypackage"
version = "1.0.0"
requires-python = ">=3.12"
dependencies = ["fastapi>=0.110", "pydantic>=2.0"]

[project.optional-dependencies]
dev = ["pytest>=8.0", "mypy>=1.10", "ruff>=0.4", "pre-commit>=3.7"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
```

## uv (Package Manager)

```bash
uv venv && uv pip install -e ".[dev]"  # Setup
uv pip compile pyproject.toml -o requirements.lock
uv run pytest                          # Run in project env
```

## Ruff (Linter + Formatter)

```toml
[tool.ruff]
target-version = "py312"
line-length = 100

[tool.ruff.lint]
select = ["E", "W", "F", "I", "N", "UP", "B", "SIM", "S", "PT", "PERF", "RUF"]
```

```bash
ruff check . && ruff format --check .  # CI
ruff check --fix . && ruff format .    # Local
```

## mypy and pytest

```toml
[tool.mypy]
python_version = "3.12"
strict = true

[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"
addopts = ["-ra", "--strict-markers", "-x"]

[tool.coverage.report]
fail_under = 80
```

## CI Essentials

```bash
ruff format --check . && ruff check . && mypy . && pytest --cov && pip-audit .
```

## Dependency Hygiene

- **Pin for applications** (lock file); **range for libraries** (`>=1.0,<2.0`)
- Audit regularly with `pip-audit`; minimize dependencies
- Prefer stdlib: `pathlib` over `os.path`, `tomllib` over `toml`
