uv/crates/pep440-rs
konsti dc94b4836f
Use backticks in pep508-rs (#6415)
In accordance with the style guide, use backticks in pep508-rs error
messages.
2024-08-22 08:31:04 +00:00
..
python Use prettier to format the documentation (#5708) 2024-08-02 08:58:31 -05:00
src Use backticks in pep508-rs (#6415) 2024-08-22 08:31:04 +00:00
test Extend Ruff configuration to sort imports (#5528) 2024-07-28 21:49:28 +00:00
Cargo.lock Copy over pep440-rs crate (#30) 2023-10-06 20:11:52 -04:00
Cargo.toml Upgrade to Rust 1.80.0 (#5472) 2024-07-27 01:49:47 +00:00
CHANGELOG.md Use prettier to format the documentation (#5708) 2024-08-02 08:58:31 -05:00
License-Apache Copy over pep440-rs crate (#30) 2023-10-06 20:11:52 -04:00
License-BSD Copy over pep440-rs crate (#30) 2023-10-06 20:11:52 -04:00
Readme.md Use prettier to format the documentation (#5708) 2024-08-02 08:58:31 -05:00

PEP440 in rust

Crates.io PyPI

A library for python version numbers and specifiers, implementing PEP 440. See Reimplementing PEP 440 for some background.

Higher level bindings to the requirements syntax are available in pep508_rs.

use std::str::FromStr;
use pep440_rs::{parse_version_specifiers, Version, VersionSpecifier};

let version = Version::from_str("1.19").unwrap();
let version_specifier = VersionSpecifier::from_str("==1.*").unwrap();
assert!(version_specifier.contains(&version));
let version_specifiers = parse_version_specifiers(">=1.16, <2.0").unwrap();
assert!(version_specifiers.contains(&version));

In python (pip install pep440_rs):

from pep440_rs import Version, VersionSpecifier

assert Version("1.1a1").any_prerelease()
assert Version("1.1.dev2").any_prerelease()
assert not Version("1.1").any_prerelease()
assert VersionSpecifier(">=1.0").contains(Version("1.1a1"))
assert not VersionSpecifier(">=1.1").contains(Version("1.1a1"))
# Note that python comparisons are the version ordering, not the version specifiers operators
assert Version("1.1") >= Version("1.1a1")
assert Version("2.0") in VersionSpecifier("==2")

PEP 440 has a lot of unintuitive features, including:

  • An epoch that you can prefix the version which, e.g. 1!1.2.3. Lower epoch always means lower version (1.0 <=2!0.1)
  • post versions, which can be attached to both stable releases and pre-releases
  • dev versions, which can be attached to sbpth table releases and pre-releases. When attached to a pre-release the dev version is ordered just below the normal pre-release, however when attached to a stable version, the dev version is sorted before a pre-releases
  • pre-release handling is a mess: "Pre-releases of any kind, including developmental releases, are implicitly excluded from all version specifiers, unless they are already present on the system, explicitly requested by the user, or if the only available version that satisfies the version specifier is a pre-release.". This means that we can't say whether a specifier matches without also looking at the environment
  • pre-release vs. pre-release incl. dev is fuzzy
  • local versions on top of all the others, which are added with a + and have implicitly typed string and number segments
  • no semver-caret (^), but a pseudo-semver tilde (~=)
  • ordering contradicts matching: We have e.g. 1.0+local > 1.0 when sorting, but ==1.0 matches 1.0+local. While the ordering of versions itself is a total order the version matching needs to catch all sorts of special cases