Skip to main content

Parse, compare, bump, and validate semantic version strings

Project description

philiprehberger-semver

Tests PyPI version Last updated

Parse, compare, bump, and validate semantic version strings.

Installation

pip install philiprehberger-semver

Usage

from philiprehberger_semver import parse, bump, compare, satisfies, is_valid, sort_versions, next_pre

Parsing

v = parse("1.2.3-beta.1+build.42")
v.major  # 1
v.minor  # 2
v.patch  # 3
v.pre    # "beta.1"
v.build  # "build.42"
str(v)   # "1.2.3-beta.1+build.42"

A leading v prefix is accepted:

parse("v2.0.0")  # Version(major=2, minor=0, patch=0)

Bumping

bump("1.2.3", "major")  # "2.0.0"
bump("1.2.3", "minor")  # "1.3.0"
bump("1.2.3", "patch")  # "1.2.4"

Comparing

compare("1.2.3", "1.3.0")  # -1
compare("2.0.0", "2.0.0")  #  0
compare("3.0.0", "2.9.9")  #  1

Range matching

satisfies("1.5.0", ">=1.0.0 <2.0.0")  # True
satisfies("1.2.5", "^1.2.3")           # True  (>=1.2.3 <2.0.0)
satisfies("1.2.5", "~1.2.3")           # True  (>=1.2.3 <1.3.0)
satisfies("1.3.0", "~1.2.3")           # False

Validation

is_valid("1.2.3")        # True
is_valid("not-a-version") # False

Sorting

sort_versions(["2.0.0", "1.0.0", "1.1.0"])  # ["1.0.0", "1.1.0", "2.0.0"]

Range expansion

Get the lower (inclusive) and upper (exclusive) bounds of a range:

from philiprehberger_semver import expand_range

expand_range("^1.2.3")      # (Version(1,2,3), Version(2,0,0))
expand_range("~1.2.3")      # (Version(1,2,3), Version(1,3,0))
expand_range(">=1.0.0")     # (Version(1,0,0), None)
expand_range(">=1 <2")      # (Version(1,0,0), Version(2,0,0))

Pre-release generation

next_pre("1.2.3")         # "1.2.4-rc.1"
next_pre("1.2.4-rc.1")    # "1.2.4-rc.2"
next_pre("1.2.3", "beta") # "1.2.4-beta.1"

API

Function Description
parse(version) Parse a semver string into a Version object
bump(version, part) Bump major, minor, or patch and return the new version string
compare(a, b) Compare two version strings, returns -1, 0, or 1
satisfies(version, range_str) Check if a version satisfies a range (>=, <, ^, ~)
is_valid(version) Check if a string is valid semver
sort_versions(versions) Sort a list of version strings in ascending order
next_pre(version, prefix) Generate the next pre-release version string
expand_range(range_str) Return (lower_inclusive, upper_exclusive) bounds of a range

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_semver-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_semver-0.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_semver-0.2.0.tar.gz.

File metadata

  • Download URL: philiprehberger_semver-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_semver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e0ba78fbfce821fae9bdee46248ba76aafb4abc519088568a220a9767edea983
MD5 fdd8731244ff7826eff91769a638ca3b
BLAKE2b-256 1e31bfb59fdee5a8d0199c89ae14ee5e5a3c294292c2a7c49e458bc344fa3c6c

See more details on using hashes here.

File details

Details for the file philiprehberger_semver-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_semver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93cd0f5a35c982abd9ce03c980c23221e3016656673cd951261a842fbd94093b
MD5 8c108a78e8412ffaab373258e09c6858
BLAKE2b-256 0a5cc5e2c672b70aa494c8ba5096f7df87a64d387b75f9df958d3415b59b9a99

See more details on using hashes here.

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