opyebSemVerLab

PEP 440 ~= with post and dev: three rules in one operator

PEP 440 · 9 min read

In one line: How ~=1.4.2 computes its upper bound, how dev/pre/post order, and what epochs, local versions and .* do.

Python version constraints differ from SemVer in grammar and in ordering. For the same 1.4.2, 1.4.2rc1 < 1.4.2 < 1.4.2.post1, and 1!1.0 is greater than 2.0. Five operators cover everyday work.

~= compatible release

ConstraintEquivalentAccepts
~=1.4.2>=1.4.2, ==1.4.*1.4.2 up to (not including) 1.5.0
~=1.4>=1.4, ==1.*1.4 up to (not including) 2.0
~=1.4.5.0>=1.4.5.0, ==1.4.5.*1.4.5.0 up to (not including) 1.4.6

The rule is one sentence: fix the prefix and drop the last release segment. Note that ~=1.4 covers all of 1.x, unlike npm's ~1.4 which covers only 1.4.x.

Ordering: dev < pre < release < post

1.0.dev1 < 1.0a1 < 1.0a1.post1 < 1.0b1 < 1.0rc1 < 1.0 < 1.0.post1 < 1.0.1

A dev release is "not built yet", so it sorts before the prereleases of the same release. A post release is a patch published after the release. 1.0-1 is another spelling of 1.0.post1.

Prereleases are excluded by default

PEP 440 recommends excluding prereleases unless a specifier mentions one, and pip follows that. So ~=1.4.2 does not accept 1.4.3.dev0. To admit prereleases, name one in the constraint (>=1.4.2.dev0) or use a tool's "allow prereleases" option.

==, !=, .* and ===

  • ==1.4.2: exactly 1.4.2. With no local label in the constraint the local part is ignored, so 1.4.2+local also matches; write ==1.4.2+local to pin it.
  • ==1.4.*: every version whose release starts with 1.4, including 1.4.2.post1.
  • !=1.4.2, !=1.4.*: exclusions, combinable with commas.
  • ===1.4.2: compares the normalised string exactly; use it only for non-standard version strings.

Epochs and local versions

1! in 1!1.0 is the epoch. A higher epoch always wins regardless of the release numbers. A local version (+ubuntu.1) is used only for ordering and is ignored by >= and <=. >1.0 excludes post releases and local versions of the same release, and <1.0 excludes prereleases of the same release. Those family exclusions make naive interval arithmetic disagree with the real answer.

Everyday mistakes

  • Reading ~=1.4.2 as "1.4.x" and expecting 1.5.0: the upper bound is 1.5.0.
  • Expecting 1.0rc1 to satisfy >=1.0: prereleases are excluded by default.
  • Expecting >1.0 to accept 1.0+local: local versions of the same release are excluded.

Paste the same constraints and candidate versions into this page to see which versions survive.

Open SemVerLab

Frequently asked questions

Why is ~=1 invalid?

A compatible release needs at least two release segments to compute an upper bound.

Does ==1.4.* accept prereleases?

The prefix matches, but the prerelease policy still applies: without a prerelease in the constraint, 1.4.3.dev0 is excluded.

Is a post release a prerelease?

No. post is a patch published after the release and is treated as stable. Only dev and pre releases are prereleases.

Sources

Read next