Note: This PEP has been superseded by the version identification and dependency specification scheme defined in PEP 440.
This PEP proposed a new version comparison schema system in Distutils.
In Python there are no real restrictions yet on how a project should manage its versions, and how they should be incremented.
Distutils provides a version distribution meta-data field but it is freeform and current users, such as PyPI usually consider the latest version pushed as the latest one, regardless of the expected semantics.
Distutils will soon extend its capabilities to allow distributions to express a dependency on other distributions through the Requires-Dist metadata field (see PEP 345) and it will optionally allow use of that field to restrict the dependency to a set of compatible versions. Notice that this field is replacing Requires that was expressing dependencies on modules and packages.
The Requires-Dist field will allow a distribution to define a dependency on another package and optionally restrict this dependency to a set of compatible versions, so one may write:
This means that the distribution requires zope.interface with a version greater than 3.5.0.
This also means that Python projects will need to follow the same convention as the tool that will be used to install them, so they are able to compare versions.
That is why this PEP proposes, for the sake of interoperability, a standard schema to express version information and its comparison semantics.
Furthermore, this will make OS packagers’ work easier when repackaging standards compliant distributions, because as of now it can be difficult to decide how two distribution versions compare.
It is not in the scope of this PEP to provide a universal versioning schema intended to support all or even most of existing versioning schemas. There will always be competing grammars, either mandated by distro or project policies or by historical reasons that we cannot expect to change.
The proposed schema should be able to express the usual versioning semantics, so it’s possible to parse any alternative versioning schema and transform it into a compliant one. This is how OS packagers usually deal with the existing version schemas and is a preferable alternative than supporting an arbitrary set of versioning schemas.
Conformance to usual practice and conventions, as well as a simplicity are a plus, to ease frictionless adoption and painless transition. Practicality beats purity, sometimes.
Projects have very different versioning needs, but the following are widely considered important semantics:
For people that want to go further and use a tool to manage their version numbers, the two major ones are:
Distutils currently provides a StrictVersion and a LooseVersion class that can be used to manage versions.
The LooseVersion class is quite lax. From Distutils doc:
This class makes any version string valid, and provides an algorithm to sort them numerically then lexically. It means that anything can be used to version your project:
The problem with this is that while it allows expressing any nesting level it doesn’t allow giving special meaning to versions (pre and post-releases as well as development versions), as expressed in requisites 2, 3 and 4.
The StrictVersion class is more strict. From the doc:
This class enforces a few rules, and makes a decent tool to work with version numbers:
It adds pre-release versions, and some structure, but lacks a few semantic elements to make it usable, such as development releases or post-release tags, as expressed in requisites 3 and 4.
Also, note that Distutils version classes have been present for years but are not really used in the community.
Setuptools provides another version comparison tool [3] which does not enforce any rules for the version, but tries to provide a better algorithm to convert the strings to sortable keys, with a parse_version function.
From the doc:
In other words, parse_version will return a tuple for each version string, that is compatible with StrictVersion but also accept arbitrary version and deal with them so they can be compared:
In this schema practicality takes priority over purity, but as a result it doesn’t enforce any policy and leads to very complex semantics due to the lack of a clear standard. It just tries to adapt to widely used conventions.
The major problem with the described version comparison tools is that they are too permissive and, at the same time, aren’t capable of expressing some of the required semantics. Many of the versions on PyPI [4] are obviously not useful versions, which makes it difficult for users to grok the versioning that a particular package was using and to provide tools on top of PyPI.
Distutils classes are not really used in Python projects, but the Setuptools function is quite widespread because it’s used by tools like easy_install [6], pip [5] or zc.buildout [7] to install dependencies of a given project.
While Setuptools does provide a mechanism for comparing/sorting versions, it is much preferable if the versioning spec is such that a human can make a reasonable attempt at that sorting without having to run it against some code.
Also there’s a problem with the use of dates at the “major” version number (e.g. a version string “20090421”) with RPMs: it means that any attempt to switch to a more typical “major.minor…” version scheme is problematic because it will always sort less than “20090421”.
Last, the meaning of - is specific to Setuptools, while it is avoided in some packaging systems like the one used by Debian or Ubuntu.
During Pycon, members of the Python, Ubuntu and Fedora community worked on a version standard that would be acceptable for everyone.
It’s currently called verlib and a prototype lives at [9].
The pseudo-format supported is:
The real regular expression is:
Some examples probably make it clearer:
The trailing .dev123 is for pre-releases. The .post123 is for post-releases – which apparently are used by a number of projects out there (e.g. Twisted [8]). For example, after a 1.2.0 release there might be a 1.2.0-r678 release. We used post instead of r because the r is ambiguous as to whether it indicates a pre- or post-release.
.post456.dev34 indicates a dev marker for a post release, that sorts before a .post456 marker. This can be used to do development versions of post releases.
Pre-releases can use a for “alpha”, b for “beta” and c for “release candidate”. rc is an alternative notation for “release candidate” that is added to make the version scheme compatible with Python’s own version scheme. rc sorts after c:
Note that c is the preferred marker for third party projects.
verlib provides a NormalizedVersion class and a suggest_normalized_version function.
The NormalizedVersion class is used to hold a version and to compare it with others. It takes a string as an argument, that contains the representation of the version:
The version can be represented as a string:
Or compared with others:
A class method called from_parts is available if you want to create an instance by providing the parts that composes the version.
Examples
suggest_normalized_version is a function that suggests a normalized version close to the given version string. If you have a version string that isn’t normalized (i.e. NormalizedVersion doesn’t like it) then you might be able to get an equivalent (or close) normalized version from this function.
This does a number of simple normalizations to the given string, based on an observation of versions currently in use on PyPI.
Given a dump of those versions on January 6th 2010, the function has given those results out of the 8821 distributions PyPI had:
The 3.20% of projects that are incompatible with NormalizedVersion and cannot be transformed into a compatible form, are for most of them date-based version schemes, versions with custom markers, or dummy versions. Examples:
When a tool needs to work with versions, a strategy is to use suggest_normalized_version on the versions string. If this function returns None, it means that the provided version is not close enough to the standard scheme. If it returns a version that slightly differs from the original version, it’s a suggested normalized version. Last, if it returns the same string, it means that the version matches the scheme.
Here’s an example of usage:
Distutils will deprecate its existing versions class in favor of NormalizedVersion. The verlib module presented in this PEP will be renamed to version and placed into the distutils package.
Trent Mick, Matthias Klose, Phillip Eby, David Lyon, and many people at Pycon and Distutils-SIG.
This document has been placed in the public domain.
Source: https://github.com/python/peps/blob/main/peps/pep-0386.rst
Last modified: 2024-12-15 20:57:19 UTC