Skip to main content

Python wrapper around the Nerdbank.GitVersioning CLI

Project description

nbgv-python Package Overview

Overview

  • nbgv-python wraps the Nerdbank.GitVersioning CLI so Python projects can reuse the same versioning semantics.
  • The package discovers the CLI via NBGV_PYTHON_COMMAND, a direct nbgv executable, or dotnet tool run nbgv as 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 nbgv CLI is available either as a global .NET tool (dotnet tool install -g nbgv) or via a local tool manifest.
  • Optionally set NBGV_PYTHON_COMMAND when the executable lives outside of PATH or 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 example 2!1.2.3). Leave it unset or null to 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-fields table. 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"]) – when mode = "nbgv", this ordered list determines which NBGV variables populate the tuple. Empty strings and null values are skipped.
    • version-tuple.normalized-prerelease (default: false) – when mode = "nbgv", convert the PrereleaseVersionNoLeadingHyphen entry to its PEP 440 shorthand (for example beta.1b1).
    • version-tuple.epoch (default: null) – only applies when mode = "pep440". Keep it null to emit the epoch only when the source string already includes a non-zero epoch (e.g., 2!1.0.0), set true to force inclusion, or false to 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 by nbgv get-version.

    • version relays the raw value selected by version-field (defaults to SemVer2).
    • normalized_version applies PEP 440 normalization to that value using packaging.version.Version so 1.2.3-beta.1 becomes 1.2.3b1.
    • version_tuple expands into a Python tuple using either the selected NBGV variables (mode = "nbgv", respecting normalized-prerelease when enabled) or the parsed PEP 440 segments (mode = "pep440").

Writing Version Files

  • Emit a templated artifact (for example src/pkg/_version.py) during build. The write table accepts the following keys:

    • file (required) – relative or absolute path of the generated file.
    • template (optional) – custom format string. When omitted, .py files default to __version__ = "{normalized_version}" when that field is available (otherwise version is used), while .txt or 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 template is omitted the plugin mirrors versioningit: .py files render __version__ = "{normalized_version}" (falling back to version if normalization was not provided), while .txt or 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbgv_python-1.1.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbgv_python-1.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file nbgv_python-1.1.0.tar.gz.

File metadata

  • Download URL: nbgv_python-1.1.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nbgv_python-1.1.0.tar.gz
Algorithm Hash digest
SHA256 c07563cb996e719b6ce85351823c447e39944249aa0782727d09f0efd22c6ff2
MD5 d2aaac3fe886cdd1768eacdcfac1036e
BLAKE2b-256 32e4a78292bc187dc2773c30e4bc9eb750f461ec248236fe259900e529874d64

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbgv_python-1.1.0.tar.gz:

Publisher: official.yml on hcoona/three

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nbgv_python-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nbgv_python-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nbgv_python-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60f2c015904b54fd89176ac90f7ad1f53b58e2e67193b992f0c10623fa01926f
MD5 572f14f384c14608ea2cfcbec8b29338
BLAKE2b-256 84a307395374fa2f503c143ef0979507e5c5f619b70727a5b7db43f12cf0d36f

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbgv_python-1.1.0-py3-none-any.whl:

Publisher: official.yml on hcoona/three

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page