PyPiLibrary
Python PyPI template - companion to the .NET NuGetLibrary in this repo. Published to PyPI as ptr727-projecttemplate-library.
Stack
- Build backend -
hatchlingviapyproject.toml - Env / deps / publish -
uv(Astral) - Lint + format -
ruff - Type checker -
pyright - Tests -
pytest - Publish - PyPI Trusted Publishing via
pypa/gh-action-pypi-publish(no API token in repo secrets) - Version - Nerdbank.GitVersioning (NBGV) shared with the .NET side. CI replaces the
__version__line in_version.py(in place) beforeuv build. Branch-aware: onmainthe value is NBGV'sAssemblyFileVersion(Major.Minor.Patch.BuildNumber, PEP 440 release); ondevelopit'sMajor.Minor.Patch.BuildNumber.dev0(PEP 440 dev release -pip installfilters the.devsuffix unless--preis passed; the BuildNumber stays in the release segment so develop's segment grows past main's per commit and--preactually prefers develop). Matches how NuGet/Docker tag develop builds as prerelease. All four artifact families (.NET assemblies, NuGet, Docker, PyPI) derive from the same NBGV computation per commit; only the formatting differs.
Layout
PyPiLibrary/
pyproject.toml
README.md
src/
ptr727_projecttemplate_library/
__init__.py
_version.py
example.py
tests/
__init__.py
test_example.py
Local Development
The repo's devcontainer installs uv automatically and runs uv sync for this project on first open. To work outside the devcontainer:
# from the repo root
cd PyPiLibrary
uv sync # creates .venv, installs deps + dev group
uv run ruff check # lint
uv run ruff format --check # formatting check
uv run pyright # type check
uv run pytest # tests
uv build # wheel + sdist into ./dist
Publishing
Releases are produced by .github/workflows/build-pypilibrary-task.yml (called from build-release-task.yml to build, lint, type-check, test, and upload the wheel + sdist as a workflow-run artifact). Publishing is a separate top-level publish-pypi job in publish-release.yml that downloads the artifact by name and runs Trusted Publishing - no PYPI_API_TOKEN secret is involved. The publish job has id-token: write only at that single job level, so the test-pull-request flow (which calls the same build task during PR validation) doesn't need to propagate that permission through the reusable workflow chain.
Two-channel publishing: pushes to both main and develop trigger publish-release.yml, and the "Compute PyPI version step" in build-pypilibrary-task.yml formats the version per branch:
main->Major.Minor.Patch.BuildNumber(PEP 440 release).pip install ptr727-projecttemplate-librarypicks this up by default.develop->Major.Minor.Patch.BuildNumber.dev0(PEP 440 dev release). The BuildNumber stays in the release segment so develop's release segment grows past main's per commit - that's what letspip install --pre ptr727-projecttemplate-libraryactually resolve to a develop build (--prewould otherwise still pick the higher-on-release-segments main version). Same PyPI project; no separate "test" project required.
Edge case worth knowing: in the window between a release merge to main and the next commit on develop, develop's BuildNumber equals main's (or is one lower), so --pre will still resolve to the main release until a new develop commit lands. Self-healing.
This matches how NuGet (NBGV SemVer2 prerelease tags), Docker (NBGV SemVer2 image tags), and GitHub releases (softprops prerelease: true on develop) already mark develop builds.
First-time setup (one-time, on PyPI):
Prerequisite: enable 2FA on the PyPI account (TOTP or hardware key). PyPI requires it before any trusted publisher can be registered.
- PyPI -> Account settings -> Publishing -> Add a new pending publisher (direct link). If the project already exists on PyPI, go to the project page -> Manage -> Publishing -> Add a new publisher instead - the "pending" form is only for projects that don't exist yet. Fields:
- PyPI project name:
ptr727-projecttemplate-library - Owner:
ptr727 - Repository name:
ProjectTemplate - Workflow filename:
publish-release.yml - Environment name:
pypi
- PyPI project name:
- GitHub repo -> Settings -> Environments -> New environment ->
pypi. The environment owns deploy-time guardrails:- Deployment branch rule -> Selected branches and tags -> add both
main(release channel) anddevelop(prerelease channel). This step is mandatory - Trusted Publishing without a branch restriction is a documented security anti-pattern. Any other branch (feature branches, codegen, etc.) is blocked at the env gate even if a workflow misconfiguration ever tried to publish from it. - (Optional) add yourself as a required reviewer so each publish requires a click - useful belt-and-suspenders against an accidental release.
- Deployment branch rule -> Selected branches and tags -> add both
- The first successful release converts the pending publisher to a real publisher. After that the same OIDC exchange validates against the real publisher on every release.
Troubleshooting:
invalid-publisher: ... Publisher with matching claims was not found- the publisher hasn't been registered yet, or one of the five claim fields (owner, repo, workflow filename, environment name, project name) doesn't match. Re-check step 1.manifest unknownfromdocker:pullingghcr.io/pypa/gh-action-pypi-publish- the SHA pinned inpublish-release.ymldoesn't correspond to a release tag with a published GHCR image. Pin to the SHA that the upstream tag (# vX.Y.Zcomment) actually points at onpypa/gh-action-pypi-publish.
Fallback (API token instead of Trusted Publishing): drop the id-token: write permission from the publish-pypi job, add password: ${{ secrets.PYPI_API_TOKEN }} to the pypa/gh-action-pypi-publish step, and store the token as a repo secret. Also pass attestations: false since attestations require the OIDC token. The OIDC path is preferred - no long-lived secret in the repo - so use the token method only when Trusted Publishing isn't an option.
Template Adoption
When deriving a new project from this template:
-
Replace the package name
ptr727-projecttemplate-library(inpyproject.toml, this README, and CI) with your name. -
Rename
src/ptr727_projecttemplate_library/to your import name. -
Re-register the trusted publisher on PyPI under the new project name.
-
Pick a versioning scheme. The template defaults to NBGV-driven versioning shared with the .NET side:
_version.pyholds__version__ = "0.0.0"as a local-development placeholder, and the CI steps "Compute PyPI version step" + "Write version into _version.py step" inbuild-pypilibrary-task.ymlcompute and rewrite the value beforeuv build. The version is branch-aware:mainpushes shipM.N.P.B(PEP 440 release),developpushes shipM.N.P.B.dev0(PEP 440 dev release - same release segment as main,.dev0marks it as prerelease sopip installfilters it unless--preis passed). The BuildNumber stays in the release segment so develop's segment grows past main's per commit, which is what lets--preactually prefer develop. Onmainthe PyPI version equals the .NETFileVersionstamp exactly; ondevelopit equals the sameFileVersionnumerically but with a trailing.dev0. .NET'sAssemblyVersion(a separate NBGV output) and NuGet/Docker (NBGVSemVer2) carry different strings across artifact families on both channels; all four derive from the same NBGV computation againstversion.json+ git history per commit. If you want a different scheme, replace both_version.pyand the workflow steps. Two common alternatives:hatch-vcs- derive the version from git tags. Add it to[build-system].requiresand switch[tool.hatch.version]tosource = "vcs". Drop the CI overwrite step. Pairs well with tag-driven releases and removes the NBGV dependency.- Manual bumps - edit
_version.pyin each release PR. Simplest, but easy to forget. Drop the CI overwrite step.
The publish workflow uses
skip-existing: trueso a re-upload of the same version is a no-op instead of a failure - useful when iterating on releases without bumping NBGV.
If you don't want a Python project at all, delete the PyPiLibrary/ folder, the build-pypilibrary-task.yml workflow, the build-pypilibrary job in build-release-task.yml, the publish-pypi job in publish-release.yml, and the uv block in .github/dependabot.yml.
Metadata
Release files for ptr727-projecttemplate-library 1.0.114.20500
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ptr727_projecttemplate_library-1.0.114.20500.tar.gz | 6.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ptr727_projecttemplate_library-1.0.114.20500-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.5 kB
Release files / ptr727_projecttemplate_library-1.0.114.20500.tar.gz
| Download URL | ptr727_projecttemplate_library-1.0.114.20500.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b646e86f96f96254b9489f23e94202194357f548a5e1d91fab7b2dcd114673e
|
|
BLAKE2b-256 checksum How to use checksums |
6bc6c03eda3541a69cbdabdefaf28e0b23a8c4ef2a735ea86a7a5f55940a8258
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.
Transparency logRelease files / ptr727_projecttemplate_library-1.0.114.20500-py3-none-any.whl
| Download URL | ptr727_projecttemplate_library-1.0.114.20500-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf3b662fdf259ecd9a10ca3646a35f15097c50510c92f9d58e890f405985d8d8
|
|
BLAKE2b-256 checksum How to use checksums |
0b2790011412461479ab927bc5d45b01e41db6987e28eb8bb9456e3a70420fd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.
Transparency log