Skip to content

Latest commit

 

History

History
100 lines (67 loc) · 8.01 KB

migration-guide.md

File metadata and controls

100 lines (67 loc) · 8.01 KB

Migration Guide

For a map of all org Python standards docs, start with development.md.

Use this guide to adopt the baseline in an existing repository.

Table of Contents

What the template manages

template/ and copier.yml form a Copier template: .pre-commit-config.yaml, baselines/ruff.toml, scripts/check.sh, .github/workflows/ci.yml and a starter pyproject.toml. copier update merges new releases into the repo while keeping local edits (3-way merge against the previously adopted release). CI runs through reusable workflows of github-workflows; see reusable-workflows.md.

1. Adopt the template

Install Copier with an exact pin (for example uv tool install copier==9.18.2), then from the repository root:

copier copy --trust --vcs-ref v6.0.1 gh:BehindTheMusicTree/python-project-standards . --overwrite # x-release-please-version
git add -p   # keep repo-specific hooks and settings the template overwrote

Questions (copier.yml):

  • package_name — [project] name in the starter pyproject.toml.
  • agent_guidance — see Optional agent guidance.
  • uv_sync_args — extra uv sync arguments for scripts/check.sh and ci.yml (for example --extra processing).

pyproject.toml is bootstrap-only (_skip_if_exists): rendered for new repos, never touched in existing ones. copier copy also runs a copy-only task (so it needs --trust; copier update never runs it) that creates a src/<package>/ placeholder with py.typed and tests/test_import.py when the repo has no Python package yet (an --overwrite adoption of an existing project is left alone), so check.sh passes on a fresh render. The module name is package_name lowercased with hyphens and dots turned into _; package_name must start with a letter or underscore. pyproject.toml declares [tool.setuptools.packages.find] where = ["src"] so the package builds. Move dev tools into [dependency-groups] dev (PEP 735) and merge relevant sections from template/pyproject.toml.jinja by hand (metadata, dependencies, [tool.mypy], pytest).

Commit .copier-answers.yml: it records the adopted release (_commit) and answers so later updates know what to merge.

A repo that already has its own scripts/check.sh or .github/workflows/ci.yml gets a Copier conflict on adoption: reconcile it with the template version.

First-adoption checklist

For a repo without .copier-answers.yml, scripts/check.sh only passes once the repo satisfies this contract:

  • pre-commit, ruff, mypy, pytest and pytest-cov are in [dependency-groups] dev. check.sh runs them with python -m inside the uv environment, so a globally installed binary never satisfies it, and it stops with a hint when pre-commit is missing.
  • pyproject.toml has [tool.ruff] extend = "baselines/ruff.toml" without its own line-length or select, and the [tool.mypy], [tool.coverage.run], [tool.coverage.report] and [tool.pytest.ini_options] sections of template/pyproject.toml.jinja, including files, the coverage source (a single root, see the comment there) and the coverage addopts.
  • Existing lint and test workflows are removed, or reduced to jobs ci.yml does not cover.

2. Install and validate

uv lock
bash scripts/check.sh
pre-commit install

3. Add project-specific overrides

  • Follow shared Python style notes in this repo’s docs/ where applicable (e.g. string enumerations (StrEnum)).
  • Ruff: keep [tool.ruff] extend = "baselines/ruff.toml" in pyproject.toml and put only exclude, extend-exclude, and [tool.ruff.lint] per-file-ignores / extend-per-file-ignores on top. Do not set lint.select, lint.ignore, line-length, or [tool.ruff.format] there—Ruff would override the shared baseline. Shared rule changes arrive through copier update on baselines/ruff.toml.
  • Mypy: keep the strict keys from the template [tool.mypy]; add [[tool.mypy.overrides]] only when needed (for example tests).
  • Add local pre-commit hooks for custom checks only when needed; copier update keeps them.

4. Update to a new release

copier update            # latest tag; or --vcs-ref vX.Y.Z
bash scripts/check.sh

Resolve any conflicts like a git merge and commit the result together with the updated .copier-answers.yml.

5. Migrating from v5 to v6 (single CI contract)

v6 replaces lint.yml, test.yml and this repository's reusable-pre-commit.yml with one managed ci.yml; the reusable_lint and test_workflow questions are gone.

  1. Note what .github/workflows/lint.yml and test.yml do locally. copier update deletes both, including local edits, without a conflict.
  2. Run copier update, then port those local needs into ci.yml (see Repo-specific needs). Replace services: containers with testcontainers.
  3. Delete any other workflow calling reusable-pre-commit.yml or duplicating the lint and test jobs.
  4. pyproject.toml is not updated by Copier: add [tool.mypy] files = ["."] (or your paths), the coverage addopts (--cov --cov-branch --cov-report=xml:coverage.xml), [tool.coverage.run] source set to a single root holding your packages (src, or . with omit = [".venv/*", "tests/*"] plus any tooling directory; without it, files no test imports are not measured), [tool.coverage.report] include_namespace_packages = true and pytest-cov in the dev group. Do not list several roots: coverage.yml merges same-named files across them.
  5. In .pre-commit-config.yaml, keep the local ruff and mypy hooks; their versions now come from uv.lock.
  6. Check that uv.lock is current, then run bash scripts/check.sh. Tests now run with python -P, which keeps the repository root off sys.path: if imports fail with ModuleNotFoundError, install the package (a src/ layout with [build-system]), or declare the needed directories in [tool.pytest.ini_options] pythonpath (the simpler fix for a flat layout).
  7. Update the required status checks: lint / check replaces the former lint context and Tests success the test contexts (see Required status checks).

Coverage now applies the organization standard of coverage.yml to the pull request.

6. Migrating from v4 (copied templates) to v5 (Copier)

v5 removes the copy-and-verify machinery. In each consumer:

  1. Run the adoption steps in §1.
  2. Delete STANDARDS_VERSION (replaced by _commit in .copier-answers.yml), scripts/verify-standards.sh, scripts/check_lint_baseline.py, baselines/DIGESTS and baselines/expected-mypy.json.
  3. Remove the verify-python-project-standards hook from .pre-commit-config.yaml.
  4. Agent/editor rules are no longer distributed (templates/cursor-rules/ is gone); rules each repo already copied are now owned by that repo.

Optional agent guidance

Set Copier answer agent_guidance to true to create project-owned starter guidance and a pinned drift-check workflow. Existing AGENTS.md, CLAUDE.md, and agent drift workflow files are preserved; reconcile their contents with the agent-config contract before enabling the check. The default is false for existing and new consumers.