PEP 440 ~= with post and dev: three rules in one operator
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
| Constraint | Equivalent | Accepts |
|---|---|---|
~=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, so1.4.2+localalso matches; write==1.4.2+localto pin it.==1.4.*: every version whose release starts with 1.4, including1.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.2as "1.4.x" and expecting 1.5.0: the upper bound is 1.5.0. - Expecting
1.0rc1to satisfy>=1.0: prereleases are excluded by default. - Expecting
>1.0to accept1.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 SemVerLabFrequently 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
- PEP 440 — Version Identification and Dependency Specification
- packaging — Specifiers
- packaging — Version specifiers
Read next
- The npm
^0.xtrap: why 0.3.0 falls outsideWhy^0.2.3ends at<0.3.0-0, how node-semver builds that bound, and which range to use for 0.x dependencies. - Cargo's
0.0.xand0.x: the patch is the boundaryWhy a bare version in Cargo means caret, why^0.0.3accepts only 0.0.3, and how commas and the missing||work. - Prerelease ordering and isolation:
beta.11beatsbeta.2SemVer 2.0.0 §11 precedence, and how npm and Cargo keep prereleases out of ranges unless you ask for them.