Skip to content

topmark.version.runtime

topmark / version / runtime

Compute TopMark VersionInfo and optionally convert to SemVer-ish output.

compute_version_info

compute_version_info(*, semver)

Compute the version information payload for TopMark.

Parameters:

Name Type Description Default
semver bool

If True, attempt to convert the package's PEP 440 version to a SemVer-ish representation.

required

Returns:

Type Description
VersionInfo

A VersionInfo instance.

VersionInfo
  • When semver=False, returns PEP 440 version with err=None.
VersionInfo
  • When semver=True and conversion succeeds, returns SemVer-ish version with err=None.
VersionInfo
  • When semver=True and conversion fails, returns the original PEP 440 version with version_format="pep440" and err=<conversion exception>.
Source code in src/topmark/version/runtime.py
def compute_version_info(
    *,
    semver: bool,
) -> VersionInfo:
    """Compute the version information payload for TopMark.

    Args:
        semver: If True, attempt to convert the package's PEP 440 version to a SemVer-ish
            representation.

    Returns:
        A `VersionInfo` instance.

        - When `semver=False`, returns PEP 440 version with `err=None`.
        - When `semver=True` and conversion succeeds, returns SemVer-ish version with `err=None`.
        - When `semver=True` and conversion fails, returns the original PEP 440 version with
            `version_format="pep440"` and `err=<conversion exception>`.
    """
    version_text: str = TOPMARK_VERSION
    version_format: VersionFormatLiteral = "pep440"
    err: Exception | None = None

    if semver:
        try:
            version_text = convert_pep440_to_semver(version_text)
            version_format = "semver"
        except ValueError as exc:
            # Fall back to PEP 440 and report the conversion error.
            err = exc
    return VersionInfo(
        version_text=version_text,
        version_format=version_format,
        err=err,
    )