Dynamic version generation from version control system tags supporting multiple VCS and versioning standards.
—
Core functionality for creating Version objects from components or parsing existing version strings into structured objects.
Create a Version object with complete version information including base version, pre-release stage, distance from tags, and VCS metadata.
class Version:
def __init__(
self,
base: str,
*,
stage: Optional[Tuple[str, Optional[int]]] = None,
distance: int = 0,
commit: Optional[str] = None,
dirty: Optional[bool] = None,
tagged_metadata: Optional[str] = None,
epoch: Optional[int] = None,
branch: Optional[str] = None,
timestamp: Optional[dt.datetime] = None,
concerns: Optional[Set[Concern]] = None,
vcs: Vcs = Vcs.Any
) -> NoneParameters:
base: Release segment like "1.2.3"stage: Pre-release stage as (stage_name, revision) tuple (e.g., ("rc", 1))distance: Number of commits since the last tagcommit: Commit hash/identifierdirty: True if working directory has uncommitted changestagged_metadata: Metadata from the tag itselfepoch: PEP 440 epoch numberbranch: Current branch nametimestamp: Commit timestamp as datetime objectconcerns: Set of version concerns/warningsvcs: Source version control systemUsage Examples:
from dunamai import Version, Vcs
import datetime as dt
# Basic version
version = Version("1.2.3")
# Pre-release version
version = Version("1.2.3", stage=("rc", 1))
# Development version with VCS info
version = Version(
"1.2.3",
stage=("dev", None),
distance=7,
commit="g29045e8",
dirty=True,
branch="feature/new-api",
vcs=Vcs.Git
)
# Version with epoch and metadata
version = Version(
"2.0.0",
epoch=1,
tagged_metadata="linux",
timestamp=dt.datetime.now()
)Parse existing version strings into Version objects using pattern matching and heuristics.
@classmethod
def parse(cls, version: str, pattern: Union[str, Pattern] = Pattern.Default) -> "Version"Parameters:
version: Version string to parse (e.g., "v1.2.3-rc1+g29045e8.dirty")pattern: Regular expression pattern or preset Pattern enum for parsingReturns: Version object with parsed components
Usage Examples:
from dunamai import Version, Pattern
# Parse with default pattern (supports 'v' prefix)
version = Version.parse("v1.2.3-rc1.post7.dev0+g29045e8")
print(version.base) # "1.2.3"
print(version.stage) # "rc"
print(version.revision) # 1
print(version.distance) # 7
print(version.commit) # "g29045e8"
# Parse without 'v' prefix
version = Version.parse("1.2.3-alpha.2", Pattern.DefaultUnprefixed)
# Parse with custom pattern
custom_pattern = r"^(?P<base>\d+\.\d+\.\d+)(-(?P<stage>\w+)\.(?P<revision>\d+))?$"
version = Version.parse("1.2.3-beta.5", custom_pattern)Access individual components of a Version object.
Properties:
base: str - Release segmentstage: Optional[str] - Pre-release stage namerevision: Optional[int] - Pre-release revision numberdistance: int - Commits since last tagcommit: Optional[str] - Commit identifierdirty: Optional[bool] - Uncommitted changes flagtagged_metadata: Optional[str] - Tag metadataepoch: Optional[int] - PEP 440 epochbranch: Optional[str] - Branch nametimestamp: Optional[datetime] - Commit timestampconcerns: Optional[Set[Concern]] - Version concernsvcs: Vcs - Source VCSUsage Examples:
from dunamai import Version
version = Version("1.2.3", stage=("rc", 1), distance=5, commit="g29045e8")
print(f"Base version: {version.base}") # "1.2.3"
print(f"Pre-release: {version.stage}") # "rc"
print(f"Revision: {version.revision}") # 1
print(f"Distance: {version.distance}") # 5
print(f"Commit: {version.commit}") # "g29045e8"
# Check if this is a development version
is_dev = version.distance > 0
print(f"Development version: {is_dev}") # TrueCompare Version objects using standard comparison operators.
def __eq__(self, other: Any) -> bool
def __lt__(self, other: Any) -> boolUsage Examples:
from dunamai import Version
v1 = Version("1.2.3")
v2 = Version("1.2.4")
v3 = Version("1.2.3", stage=("rc", 1))
print(v1 < v2) # True
print(v1 == v3) # False (different stage)
print(v3 < v1) # True (pre-release < release)
# Sort versions
versions = [
Version("1.2.3"),
Version("1.2.3", stage=("rc", 1)),
Version("1.2.4")
]
sorted_versions = sorted(versions)Convert Version objects to string representations.
def __str__(self) -> str
def __repr__(self) -> strUsage Examples:
from dunamai import Version
version = Version("1.2.3", stage=("rc", 1), distance=5)
print(str(version)) # Uses serialize() - "1.2.3rc1.post5.dev0"
print(repr(version)) # Detailed representation showing all attributesInstall with Tessl CLI
npx tessl i tessl/pypi-dunamai