Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Semantic Version Comparison for Python

Project description


🐍 Semantic Version Comparison for Python

Implementation of a Version object with comparison capabilities and tag validation following semver conventions.


pip install semvercomp



Version(major=0 : int, minor=0 : int, patch=0 : int, pre_release="" : str, patch="" : str, has_v=False : str)

Class Version represents a version tag following semver conventions. A version tag is structured the following way:

Value Name Description
X Major Version when you make incompatible API changes
Y Minor Version when you add functionality in a backwards-compatible manner
Z Patch Version when you make backwards-compatible bug fixes
pre Pre Release Version pre-release tag
build Build Version build tag

Source: Semantic Versioning 2.0.0

  • Public Properties
Key Value Type
has_v bool Flag for preceding v or V in version tag
major int Major member
minor int Minor member
patch int Patch member
pre_release str Pre Release member
build str Build member
  • Instance of Version
from semvercomp.Version import Version

ver = Version(1, 0, 0, 'beta', '20191224')
# 1.0.0-beta+20191224
  • Creating a Version object from a version string:
from semvercomp.Version import Version

str_v = Version()
# str_v.major == 1
# str_v.minor == 0
# str_v.patch == 0
# str_v.has_v == True
# str_v.pre_release == 'beta'

Comparison of Version Objects

Version class implements __gt__, __lt__ and __eq__ built-in methods to implement comparison.


from semvercomp.Version import Version

a = Version(1, 0, 0)
b = Version(1, 0, 0)
print(a == b) # True


from semvercomp.Version import Version

a = Version(1, 1, 0)
b = Version(1, 0, 0)
print(a > b) # True


from semvercomp.Version import Version

a = Version(0, 1, 0)
b = Version(1, 0, 0)
print(a < b) # True


It is possible to validate and gather the different members of a version tag using validate version from semvercomp.validators.

validate_version(version: str): (parts: dict(), is_ok: bool)

Will return a tuple where, the first element is a dictionary with the properties of the given version tag destructured.

The second element in the tuple is a boolean flag that stands as the validation result.

from semvercomp.validators import validate_version

ver_str = 'v1.0.22'
(parts, is_ok) = validate_version(ver_str)
print(parts) # {'has_v': True, 'major': 1, 'minor': 0, 'patch': 22, 'pre_release': None, 'build': None}
print(is_ok) # True


to_version_list(coll: iterable): Version[]

Create an array of Version objects from an iterable of version tags as strings.

from semvercomp.utils import to_version_list

all = [

versions = to_version_list(all)




  • Debugging tests Tests for this package are written with pytest. The following json, is an example of the .vscode/settings.json:
	"python.pythonPath": /* Your Python Binary Address*/,
	"python.testing.pytestArgs": [
	"python.testing.unittestEnabled": false,
	"python.testing.nosetestsEnabled": false,
	"python.testing.pytestEnabled": true


  • Running unit tests
# from repository root directory
  • Running test coverage
# from repository root directory
pytest --cov=semvercomp tests/

# or with html report
pytest --cov-report html --cov=semvercomp tests/

Project details

Download files

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

Files for semvercomp, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size semvercomp-0.1.1-py3-none-any.whl (7.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size semvercomp-0.1.1.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page