Skip to main content

npm-semver

PyPI Python tests license

Ask the question Python could not answer: does this version satisfy this range?

>>> from npm_semver import satisfies, max_satisfying
>>> satisfies("1.4.2", "^1.2.0")
True
>>> satisfies("2.0.0", "^1.2.0")
False
>>> max_satisfying(["1.2.0", "1.4.2", "2.0.0"], "^1.2.0")
Version(major=1, minor=4, patch=2, prerelease=(), build='')

semver compares versions and packaging parses them, but neither reads a range: ^1.2.0, ~1.2, 1.x, >=1.2 <2, 1.2.3 - 2.0.0, a || b. This package does, following npm's rules, including the parts people get wrong:

  • ^0.2.3 stops at 0.3.0, while ^1.2.3 stops at 2.0.0, and ^0.0.3 stops at 0.0.4.
  • >1.2 means >=1.3.0, and <=1.2 means <1.3.0.
  • 1.2 and 1.2.x mean "the 1.2 line", not "exactly 1.2".
  • A prerelease only matches a range that asks for a prerelease of the same major.minor.patch, so 1.0.0-rc.1 does not satisfy ^1.0.0.

Zero dependencies, standard library only, Python 3.9+.

Install

pip install npm-semver

Use

from npm_semver import (
    InvalidRange,
    InvalidVersion,
    max_satisfying,
    min_satisfying,
    filter_satisfying,
    parse,
    parse_range,
    satisfies,
    sort_versions,
)

satisfies("1.2.3", ">=1.2.0 <2.0.0")  # True
satisfies("1.0.0-rc.1", "^1.0.0")  # False, prerelease needs its own comparator
satisfies("2.1.0", "1.x || >=2.0.0")  # True

sort_versions(["2.0.0", "1.0.0-rc.1", "1.0.0"])
# [Version(1, 0, 0, ('rc', '1')), Version(1, 0, 0), Version(2, 0, 0)]

max_satisfying(["1.9.9", "2.0.0"], "^1.2.0")  # Version(1, 9, 9)
min_satisfying(["1.4.2", "1.9.9"], "^1.2.0")  # Version(1, 4, 2)
filter_satisfying(["1.0.0", "1.4.2", "2.0.0"], ">=1.1.0")  # [Version(1, 4, 2), Version(2, 0, 0)]

parse_range("1.2.3 - 2.0")  # ((('>=', Version(1, 2, 3)), ('<', Version(2, 1, 0))),)
parse("v1.2.3-beta.1+sha.abc").prerelease  # ('beta', '1')

Anything a range cannot express raises InvalidRange; anything that is not a version raises InvalidVersion. Both subclass ValueError.

Supported range syntax

form example means
exact 1.2.3 =1.2.3
caret ^1.2.3, ^0.2.3, ^1, ^0 compatible with, stopping at the next left-most non-zero part
tilde ~1.2.3, ~1.2, ~1 patch level, or minor level for a partial version
comparison >, >=, <, <=, = with partial-version semantics
wildcard *, 1.x, 1.2.X the whole line
partial 1.2, 1 the whole minor or major
hyphen 1.2.3 - 2.0 inclusive on both sides, upper bound widened if partial
union 1.x || >=2.0.0 any branch
conjunction >=1.2.0 <2.0.0, >=1.2.0, <2.0.0 all comparators

Build metadata (+sha) is parsed and printed but ignored in comparisons, as semver requires.

Compatibility

npm-semver deliberately does not read PEP 440 ranges (~=1.2, ==1.*, epochs). If you need those, use packaging. Ranges here are npm's, which is what JavaScript ecosystems, lockfiles and package.json emit.

Development

python -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest

License

MIT.

Metadata

Release files for npm-semver 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for npm-semver 0.1.1
File Size Uploaded Provenance Actions
npm_semver-0.1.1.tar.gz 8.8 kB Without provenance Details for npm_semver-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for npm-semver 0.1.1
File Interpreter ABI Platform Actions
npm_semver-0.1.1-py3-none-any.whl Python 3 none any Details for npm_semver-0.1.1-py3-none-any.whl

Total release size: 15.9 kB

Release files / npm_semver-0.1.1.tar.gz

Download URL npm_semver-0.1.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1c3b8d98f3f52b35849ec3e792ba621689afe1f5375c3478de87db62cec043de
BLAKE2b-256 checksum
How to use checksums
cc651b3293bb58f725bd1d868b289369f528c991aa57afc5a34d7edc58bb70f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / npm_semver-0.1.1-py3-none-any.whl

Download URL npm_semver-0.1.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ab5d65256a0b32ea6b28e38c0a6b4692cc85cd51194e142119c45b65bffe4e0
BLAKE2b-256 checksum
How to use checksums
98f3e3b0224a5c8138d8e784d6170fdc26fe9cb1ed5ae568eeb125988be78308
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for npm-semver 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page