Semantic Versioning Library
This Python library provides tools to handle Semantic Versioning (SemVer) strictly following the 2.0.0 scheme. It offers classes for parsing, comparing, and manipulating version numbers and defining requirement specifications. As of version 2.10.0, it is actively maintained with a stable release cadence.
Warnings
- gotcha The library strictly adheres to Semantic Versioning 2.0.0. Attempting to parse or validate strings that do not conform to this specification will raise a `ValueError`.
- gotcha Build metadata (the part after `+`, e.g., `+build.123`) is ignored when determining version precedence. This means `Version('1.0.0+abc') == Version('1.0.0+xyz')` is `True`. The `compare()` function may return `NotImplemented` if versions only differ by build metadata.
- gotcha Pre-release identifiers (the part after `-`, e.g., `-alpha`) are compared lexicographically as ASCII strings, not numerically. Also, pre-release versions have lower precedence than their corresponding release version (e.g., `1.0.0-alpha < 1.0.0`).
- gotcha Using `Version(version_string, partial=True)` alters comparison behavior. If a component (minor, patch, prerelease, or build) was absent from the partial Version (represented with `None`), it is considered equal for comparison purposes. For instance, `Version('1.0', partial=True)` means 'any version beginning with 1.0'.
- gotcha The `SimpleSpec` and `NpmSpec` classes implement different version range specification schemes. `SimpleSpec` is intuitive and inspired by PyPI, while `NpmSpec` strictly follows NPM's complex rules (e.g., `~` and `^` operators have specific meanings).
Install
-
pip install semantic-version
Imports
- Version
from semantic_version import Version
- SimpleSpec
from semantic_version import SimpleSpec
- NpmSpec
from semantic_version import NpmSpec
- compare, match, validate
from semantic_version import compare, match, validate
Quickstart
from semantic_version import Version, SimpleSpec
# Create a Version object
v = Version('1.2.3-alpha+build.123')
print(f"Version: {v}")
print(f"Major: {v.major}, Minor: {v.minor}, Patch: {v.patch}")
print(f"Prerelease: {v.prerelease}, Build: {v.build}")
# Compare versions
v1 = Version('1.0.0')
v2 = Version('1.0.1')
print(f"Is {v1} < {v2}? {v1 < v2}")
# Define and use a SimpleSpec
spec = SimpleSpec('>=1.0.0,<2.0.0')
print(f"Does {v1} match spec '>1.0.0,<2.0.0'? {v1 in spec}")
print(f"Does {v2} match spec '>1.0.0,<2.0.0'? {v2 in spec}")
print(f"Does Version('2.0.0') match spec '>1.0.0,<2.0.0'? {Version('2.0.0') in spec}")