Python wrapper around the Nerdbank.GitVersioning CLI
Project description
nbgv-python Package Overview
Overview
nbgv-pythonwraps the Nerdbank.GitVersioning CLI so Python projects can reuse the same versioning semantics.- The package discovers the CLI via
NBGV_PYTHON_COMMAND, a directnbgvexecutable, ordotnet tool run nbgvas a fallback. - A Hatch version source plugin is provided, allowing projects to declare
dynamic = ["version"]and resolve their version at build time.
Installation
- Install the package into your monorepo environment using
uv add --package nbgv-python nbgv-python. - Ensure the
nbgvCLI is available either as a global .NET tool (dotnet tool install -g nbgv) or via a local tool manifest. - Optionally set
NBGV_PYTHON_COMMANDwhen the executable lives outside ofPATHor requires additional arguments.
Hatch Integration
Add the plugin to your project configuration:
[project]
dynamic = ["version"]
[tool.hatch.version]
source = "nbgv"
[tool.hatch.version.nbgv]
version-field = "SemVer2"
During builds Hatch invokes nbgv get-version --format json in the project root and uses the selected field for the package version.
Additional configuration keys include:
command(override CLI invocation)working-directory(relative path for the repository root)epoch(default:null) – when provided, this non-negative integer is prepended to the normalized version to emit a PEP 440 epoch (for example2!1.2.3). Leave it unset ornullto omit the epoch entirely.
The plugin normalizes the chosen value to a PEP 440 compliant version, so SemVer pre-release tags such as -beta.1 are mapped to b1 automatically.
Template Fields
-
Extend the available template variables via the optional
template-fieldstable. Supported options and defaults are:version-tuple.mode(default:"nbgv") – choose how to build the tuple. Use"nbgv"(default) to assemble components from the Nerdbank.GitVersioning JSON payload, or"pep440"to parse the normalized version string.version-tuple.fields(default:["VersionMajor", "VersionMinor", "BuildNumber", "PrereleaseVersionNoLeadingHyphen"]) – whenmode = "nbgv", this ordered list determines which NBGV variables populate the tuple. Empty strings andnullvalues are skipped.version-tuple.normalized-prerelease(default:false) – whenmode = "nbgv", convert thePrereleaseVersionNoLeadingHyphenentry to its PEP 440 shorthand (for examplebeta.1→b1).version-tuple.epoch(default:null) – only applies whenmode = "pep440". Keep itnullto emit the epoch only when the source string already includes a non-zero epoch (e.g.,2!1.0.0), settrueto force inclusion, orfalseto suppress it.version-tuple.double-quote(default:true) – controls whether string parts in the tuple use double quotes ("") or single quotes ('').
[tool.hatch.version.nbgv.template-fields.version-tuple] fields = [ "VersionMajor", "VersionMinor", "BuildNumber", "PrereleaseVersionNoLeadingHyphen", ] double-quote = false
[tool.hatch.version.nbgv.template-fields.version-tuple] mode = "pep440" epoch = true
[tool.hatch.version.nbgv.template-fields.version-tuple] normalized-prerelease = true
[tool.hatch.version.nbgv] epoch = 2
-
The generated mapping includes
version,normalized_version,version_tuple, and every key exposed bynbgv get-version.versionrelays the raw value selected byversion-field(defaults toSemVer2).normalized_versionapplies PEP 440 normalization to that value usingpackaging.version.Versionso1.2.3-beta.1becomes1.2.3b1.version_tupleexpands into a Python tuple using either the selected NBGV variables (mode = "nbgv", respectingnormalized-prereleasewhen enabled) or the parsed PEP 440 segments (mode = "pep440").
Writing Version Files
-
Emit a templated artifact (for example
src/pkg/_version.py) during build. Thewritetable accepts the following keys:file(required) – relative or absolute path of the generated file.template(optional) – custom format string. When omitted,.pyfiles default to__version__ = "{normalized_version}"when that field is available (otherwiseversionis used), while.txtor extension-less files default to{version}; other suffixes require an explicit template.encoding(optional, default:"utf-8") – text encoding used when writing the file.
[tool.hatch.version.nbgv.write] file = "src/pkg/_version.py" template = "__version__ = '{version}'"
-
When
templateis omitted the plugin mirrorsversioningit:.pyfiles render__version__ = "{normalized_version}"(falling back toversionif normalization was not provided), while.txtor extension-less files render{version}. -
The target path is resolved relative to the project root and created on demand; remember to include it in your source distribution configuration if needed.
Python API
Retrieve version metadata directly from Python code:
from nbgv_python import get_version
version = get_version()
print(version["semver2"]) # -> 1.2.3+gabcdef
Forward arbitrary commands to the CLI:
from nbgv_python import forward
forward(["cloud", "--ci"])
All helpers raise NbgvNotFoundError when the CLI cannot be resolved and NbgvCommandError for non-zero exit codes.
Command-Line Usage
The console script nbgv-python proxies all arguments to the real CLI:
nbgv-python get-version --format json
The wrapper surfaces the same exit codes as the underlying tool and prints diagnostic guidance when the command cannot be located.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nbgv_python-1.1.0b1.tar.gz.
File metadata
- Download URL: nbgv_python-1.1.0b1.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddcdbe65aeddcbb2bdcde1684fd8f86e5aafbd21a9a22365e6aa7b3edd578689
|
|
| MD5 |
55bc0bb611a4f87755a56a0018641768
|
|
| BLAKE2b-256 |
4121328c148eb7a8416379b8e408398d89acbbe56e2ef8e31ab3f538608eafa0
|
File details
Details for the file nbgv_python-1.1.0b1-py3-none-any.whl.
File metadata
- Download URL: nbgv_python-1.1.0b1-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001b8e9ada9b9142b7a651d609c044fd3354fd64334e43bb597dca64a945fbc4
|
|
| MD5 |
291e63dca549652232c3befea38c59e0
|
|
| BLAKE2b-256 |
a0a68c4bbe86bf91b4346b70f82b89698ba2b8632b6aed1cdbb7aa35aa0b3ed8
|