blob: f36cdaba10c0bb1b75299a6d169a3f3f394acee6 [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001:mod:`packaging.version` --- Version number classes
2===================================================
3
4.. module:: packaging.version
5 :synopsis: Classes that represent project version numbers.
6
7
8This module contains classes and functions useful to deal with version numbers.
9It's an implementation of version specifiers `as defined in PEP 345
10<http://www.python.org/dev/peps/pep-0345/#version-specifiers>`_.
11
12
13Version numbers
14---------------
15
16.. class:: NormalizedVersion(self, s, error_on_huge_major_num=True)
17
18 A specific version of a distribution, as described in PEP 345. *s* is a
19 string object containing the version number (for example ``'1.2b1'``),
20 *error_on_huge_major_num* a boolean specifying whether to consider an
21 apparent use of a year or full date as the major version number an error.
22
23 The rationale for the second argument is that there were projects using years
24 or full dates as version numbers, which could cause problems with some
25 packaging systems sorting.
26
27 Instances of this class can be compared and sorted::
28
29 >>> NormalizedVersion('1.2b1') < NormalizedVersion('1.2')
30 True
31
32 :class:`NormalizedVersion` is used internally by :class:`VersionPredicate` to
33 do its work.
34
35
36.. class:: IrrationalVersionError
37
38 Exception raised when an invalid string is given to
39 :class:`NormalizedVersion`.
40
41 >>> NormalizedVersion("irrational_version_number")
42 ...
43 IrrationalVersionError: irrational_version_number
44
45
46.. function:: suggest_normalized_version(s)
47
48 Before standardization in PEP 386, various schemes were in use. Packaging
49 provides a function to try to convert any string to a valid, normalized
50 version::
51
52 >>> suggest_normalized_version('2.1-rc1')
53 2.1c1
54
55
56 If :func:`suggest_normalized_version` can't make sense of the given string,
57 it will return ``None``::
58
59 >>> print(suggest_normalized_version('not a version'))
60 None
61
62
63Version predicates
64------------------
65
66.. class:: VersionPredicate(predicate)
67
68 This class deals with the parsing of field values like
69 ``ProjectName (>=version)``.
70
71 .. method:: match(version)
72
73 Test if a version number matches the predicate:
74
75 >>> version = VersionPredicate("ProjectName (<1.2, >1.0)")
76 >>> version.match("1.2.1")
77 False
78 >>> version.match("1.1.1")
79 True
80
81
82Validation helpers
83------------------
84
85If you want to use :term:`LBYL`-style checks instead of instantiating the
86classes and catching :class:`IrrationalVersionError` and :class:`ValueError`,
87you can use these functions:
88
89.. function:: is_valid_version(predicate)
90
91 Check whether the given string is a valid version number. Example of valid
92 strings: ``'1.2'``, ``'4.2.0.dev4'``, ``'2.5.4.post2'``.
93
94
95.. function:: is_valid_versions(predicate)
96
97 Check whether the given string is a valid value for specifying multiple
98 versions, such as in the Requires-Python field. Example: ``'2.7, >=3.2'``.
99
100
101.. function:: is_valid_predicate(predicate)
102
103 Check whether the given string is a valid version predicate. Examples:
104 ``'some.project == 4.5, <= 4.7'``, ``'speciallib (> 1.0, != 1.4.2, < 2.0)'``.