Python + uv
Category: Tech Stack · Areas: all
Description
Category
tech-stack
Areas
all
Components
- Language: Python 3.12+
- Package manager: uv — NOT pip, NOT poetry, NOT conda
- Virtual environment: uv-managed (
.venvviauv sync) - Build backend:
hatchlingwithuv-dynamic-versioningfor versioned packages - Linter:
ruff— NOT flake8, NOT pylint - Type checker:
pyright— NOT mypy - Test framework:
pytestwithpytest-cov - Property-based testing:
hypothesis
Constraints
- All code must pass
pyrighttype checking - All code must pass
ruff checkandruff format --check - Use
pyproject.tomlfor all project metadata (notsetup.py, notsetup.cfg) - Pin Python version in
.python-version - All dependencies in
[project.dependencies]or[dependency-groups](dev); norequirements.txt - Use
[tool.uv.sources]for custom package indexes (e.g., PyTorch CUDA wheels) - Tests in
tests/directory; pytest markers for test categories (acceptance, contract, slow, fast) - Branch coverage enforced via
pytest-covwithfail_under
When to use
Python projects that benefit from fast dependency resolution and reproducible environments. Good for data services, APIs, ML pipelines, CLI tools, and libraries. uv is the single tool for venv creation, dependency resolution, and script running.
ADR References
Practices by activity
Agents working in any of these activities inherit the practices below via the bead’s context digest.
Requirements (Frame activity)
- Specify minimum Python version (3.12+ preferred)
- Identify whether the project is a library (published) or an application (not published)
- If ML/GPU dependencies exist, plan for
[tool.uv.sources]with custom package indexes
Design
- One
pyproject.tomlper project (or per package in a workspace) - Library projects: use
hatchlingbuild backend withuv-dynamic-versioningfor git-tag-based versions - Application projects:
[tool.uv] package = false(no build artifact needed) - Organize source under
src/<package_name>/layout - Use
pydanticv2 for data validation and settings models - Use
typer+richfor CLI interfaces
Implementation
- Create/sync environment:
uv sync(creates.venvautomatically) - Run scripts:
uv run python ...oruv run pytest - Add dependencies:
uv add <pkg>(notpip install) - Add dev dependencies:
uv add --dev <pkg>or to[dependency-groups] dev - Type annotations: all public functions and methods must have type annotations
- Avoid
Any— usepyrighttargeted# type: ignorewith comment when unavoidable - Use
TYPE_CHECKINGguard for import-only type imports
Testing
- Framework:
pytest - Run:
uv run pytest - Property-based:
hypothesisfor data invariants and input space exploration - Mocking:
pytest-mock(notunittest.mockdirectly) - Coverage:
pytest-covwith branch coverage; setfail_underin[tool.coverage.report] - Test markers:
acceptance,contract,slow,fast— use--strict-markers - Filter known third-party deprecation warnings in
[tool.pytest.ini_options] filterwarnings
Quality Gates (pre-commit / CI)
ruff check .— lintruff format --check .— formatpyright— type checkuv run pytest --cov— tests with coveragepre-commit run --all-filesfor the full gate
Dependency Management
uv add <pkg>/uv add --dev <pkg>- Custom indexes: declare in
[tool.uv.sources]and[[tool.uv.index]] - Lock file:
uv.lockcommitted - Do not commit
.venv/or use system Python