summaryrefslogtreecommitdiff
path: root/docs/reference.rst
blob: 1c4f0cf152d33dbd0f865a82484169dd4ae4a24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
Reference
=========


.. module:: semantic_version


Module-level functions
----------------------

.. function:: compare(v1, v2)

    Compare two version strings, and return a result similar to that of :func:`cmp`::

        >>> compare('0.1.1', '0.1.2')
        -1
        >>> compare('0.1.1', '0.1.1')
        0
        >>> compare('0.1.1', '0.1.1-alpha')
        1

    :param str v1: The first version to compare
    :param str v2: The second version to compare
    :raises: :exc:`ValueError`, if any version string is invalid
    :rtype: ``int``, -1 / 0 / 1 as for a :func:`cmp` comparison;
            ``NotImplemented`` if versions only differ by build metadata


.. warning:: Since build metadata has no ordering,
             ``compare(Version('0.1.1'), Version('0.1.1+3'))`` returns ``NotImplemented``


.. function:: match(spec, version)

    Check whether a version string matches a specification string::

        >>> match('>=0.1.1', '0.1.2')
        True
        >>> match('>=0.1.1', '0.1.1-alpha')
        False
        >>> match('~0.1.1', '0.1.1-alpha')
        True

    :param str spec: The specification to use, as a string
    :param str version: The version string to test against the spec
    :raises: :exc:`ValueError`, if the ``spec`` or the ``version`` is invalid
    :rtype: ``bool``


.. function:: validate(version)

    Check whether a version string complies with the `SemVer`_ rules.

    .. code-block:: pycon

        >>> semantic_version.validate('1.1.1')
        True
        >>> semantic_version.validate('1.2.3a4')
        False

    :param str version: The version string to validate
    :rtype: ``bool``


Representing a version (the Version class)
------------------------------------------

.. class:: Version(version_string[, partial=False])

    Object representation of a `SemVer`_-compliant version.

    Constructed from a textual version string::

        >>> Version('1.1.1')
        Version('1.1.1')
        >>> str(Version('1.1.1'))
        '1.1.1'

.. class:: Version(major: int, minor: int, patch: int, prereleases: tuple, build: tuple[, partial=False])

    Constructed from named components:

        .. code-block:: pycon

            >>> Version(major=1, minor=2, patch=3)
            Version('1.2.3')



    .. rubric:: Attributes


    .. attribute:: partial

        ``bool``, whether this is a 'partial' or a complete version number.
        Partial version number may lack :attr:`minor` or :attr:`patch` version numbers.

        .. deprecated:: 2.7
            The ability to define a partial version will be removed in version 3.0.
            Use :class:`SimpleSpec` instead: ``SimpleSpec('1.x.x')``.

    .. attribute:: major

        ``int``, the major version number

    .. attribute:: minor

        ``int``, the minor version number.

        May be ``None`` for a :attr:`partial` version number in a ``<major>`` format.

    .. attribute:: patch

        ``int``, the patch version number.

        May be ``None`` for a :attr:`partial` version number in a ``<major>`` or ``<major>.<minor>`` format.

    .. attribute:: prerelease

        ``tuple`` of ``strings``, the prerelease component.

        It contains the various dot-separated identifiers in the prerelease component.

        May be ``None`` for a :attr:`partial` version number in a ``<major>``, ``<major>.<minor>`` or ``<major>.<minor>.<patch>`` format.

    .. attribute:: build

        ``tuple`` of ``strings``, the build metadata.

        It contains the various dot-separated identifiers in the build metadata.

        May be ``None`` for a :attr:`partial` version number in a ``<major>``, ``<major>.<minor>``,
        ``<major>.<minor>.<patch>`` or ``<major>.<minor>.<patch>-<prerelease>`` format.

    .. attribute:: precedence_key

        Read-only attribute; suited for use in ``sort(versions, key=lambda v: v.precedence_key)``.
        The actual value of the attribute is considered an implementation detail; the only
        guarantee is that ordering versions by their precedence_key will comply with semver precedence rules.

        Note that the :attr:`~Version.build` isn't included in the precedence_key computatin.

    .. rubric:: Methods


    .. method:: next_major(self)

        Return the next major version, i.e the smallest version strictly greater
        than the current one with minor and patch set to 0 and no prerelease/build.

        .. code-block:: pycon

            >>> Version('1.0.2').next_major()
            Version('2.0.0')
            >>> Version('1.0.0+b3').next_major()
            Version('2.0.0')
            >>> Version('1.0.0-alpha').next_major()
            Version('1.0.0')

    .. method:: next_minor(self)

        Return the next minor version, i.e the smallest version strictly greater
        than the current one, with a patch level of ``0``.

        .. code-block:: pycon

            >>> Version('1.0.2').next_minor()
            Version('1.1.0')
            >>> Version('1.0.0+b3').next_minor()
            Version('1.1.0')
            >>> Version('1.1.2-alpha').next_minor()
            Version('1.2.0')
            >>> Version('1.1.0-alpha').next_minor()
            Version('1.1.0')

    .. method:: next_patch(self):

        Return the next patch version, i.e the smallest version strictly
        greater than the current one with empty :attr:`prerelease` and :attr:`build`.

        .. code-block:: pycon

            >>> Version('1.0.2').next_patch()
            Version('1.0.3')
            >>> Version('1.0.2+b3').next_patch()
            Version('1.0.3')
            >>> Version('1.0.2-alpha').next_patch()
            Version('1.0.2')

        .. warning:: The next patch version of a version with a non-empty
                     :attr:`prerelease` is the version without that
                     :attr:`prerelease` component: it's the smallest "pure"
                     patch version strictly greater than that version.

    .. method:: truncate(self, level='patch']):

        Returns a similar level, but truncated at the provided level.

        .. code-block:: pycon

            >>> Version('1.0.2-rc1+b43.24').truncate()
            Version('1.0.2')
            >>> Version('1.0.2-rc1+b43.24').truncate('minor')
            Version('1.0.0')
            >>> Version('1.0.2-rc1+b43.24').truncate('prerelease')
            Version('1.0.2-rc1')


    .. method:: __iter__(self)

        Iterates over the version components (:attr:`major`, :attr:`minor`,
        :attr:`patch`, :attr:`prerelease`, :attr:`build`)::

            >>> list(Version('0.1.1'))
            [0, 1, 1, [], []]

        .. note:: This may pose some subtle bugs when iterating over a single version
                  while expecting an iterable of versions -- similar to::

                      >>> list('abc')
                      ['a', 'b', 'c']
                      >>> list(('abc',))
                      ['abc']


    .. method:: __cmp__(self, other)

        Provides comparison methods with other :class:`Version` objects.

        The rules are:

        - For non-:attr:`partial` versions, compare using the `SemVer`_ scheme
        - If any compared object is :attr:`partial`:
            - Begin comparison using the `SemVer`_ scheme
            - If a component (:attr:`minor`, :attr:`patch`, :attr:`prerelease` or :attr:`build`)
              was absent from the :attr:`partial` :class:`Version` -- represented with :obj:`None`
              --, consider both versions equal.

            For instance, ``Version('1.0', partial=True)`` means "any version beginning in ``1.0``".

            ``Version('1.0.1-alpha', partial=True)`` means "The ``1.0.1-alpha`` version or any
            any release differing only in build metadata": ``1.0.1-alpha+build3`` matches, ``1.0.1-alpha.2`` doesn't.

        Examples::

              >>> Version('1.0', partial=True) == Version('1.0.1')
              True
              >>> Version('1.0.1-rc1.1') == Version('1.0.1-rc1', partial=True)
              False
              >>> Version('1.0.1-rc1+build345') == Version('1.0.1-rc1')
              False
              >>> Version('1.0.1-rc1+build345') == Version('1.0.1-rc1', partial=True)
              True


    .. method:: __str__(self)

        Returns the standard text representation of the version::

            >>> v = Version('0.1.1-rc2+build4.4')
            >>> v
            Version('0.1.1-rc2+build4.4')
            >>> str(v)
            '0.1.1-rc2+build4.4'


    .. method:: __hash__(self)

        Provides a hash based solely on the components.

        Allows using a :class:`Version` as a dictionary key.

        .. note:: A fully qualified :attr:`partial` :class:`Version`

                  (up to the :attr:`build` component) will hash the same as the
                  equally qualified, non-:attr:`partial` :class:`Version`::

                      >>> hash(Version('1.0.1+build4')) == hash(Version('1.0.1+build4', partial=True))
                      True


    .. rubric:: Class methods


    .. classmethod:: parse(cls, version_string[, partial=False])

        Parse a version string into a ``(major, minor, patch, prerelease, build)`` tuple.

        :param str version_string: The version string to parse
        :param bool partial: Whether this should be considered a :attr:`partial` version
        :raises: :exc:`ValueError`, if the :attr:`version_string` is invalid.
        :rtype: (major, minor, patch, prerelease, build)

    .. classmethod:: coerce(cls, version_string[, partial=False])

        Try to convert an arbitrary version string into a :class:`Version` instance.

        Rules are:

        - If no minor or patch component, and :attr:`partial` is :obj:`False`,
          replace them with zeroes
        - Any character outside of ``a-zA-Z0-9.+-`` is replaced with a ``-``
        - If more than 3 dot-separated numerical components, everything from the
          fourth component belongs to the :attr:`build` part
        - Any extra ``+`` in the :attr:`build` part will be replaced with dots

        Examples:

        .. code-block:: pycon

          >>> Version.coerce('02')
          Version('2.0.0')
          >>> Version.coerce('1.2.3.4')
          Version('1.2.3+4')
          >>> Version.coerce('1.2.3.4beta2')
          Version('1.2.3+4beta2')
          >>> Version.coerce('1.2.3.4.5_6/7+8+9+10')
          Version('1.2.3+4.5-6-7.8.9.10')

        :param str version_string: The version string to coerce
        :param bool partial: Whether to allow generating a :attr:`partial` version
        :raises: :exc:`ValueError`, if the :attr:`version_string` is invalid.
        :rtype: :class:`Version`


Version specifications (the Spec class)
---------------------------------------


Version specifications describe a 'range' of accepted versions:
older than, equal, similar to, …

python-semanticversion supports different syntaxes for describing version range:

- ``'simple'`` (through :class:`SimpleSpec`): A python-semantic specific syntax, which supports common patterns, and some NPM-inspired extensions;
- ``'npm'`` (through :class:`NpmSpec`): The NPM syntax, based on https://docs.npmjs.com/misc/semver.html


.. class:: BaseSpec(spec_string)

    Converts an expression describing a range of versions into a set of clauses,
    and matches any :class:`Version` against those clauses.


    .. rubric:: Attributes

    This class has no public attributes.

    .. rubric:: Methods


    .. method:: match(self, version)

        Test whether a given :class:`Version` matches all included :class:`SpecItem`::

            >>> Spec('>=1.1.0,<1.1.2').match(Version('1.1.1'))
            True

        :param version: The version to test against the specs
        :type version: :class:`Version`
        :rtype: ``bool``


    .. method:: filter(self, versions)

        Extract all compatible :class:`versions <Version>` from an iterable of
        :class:`Version` objects.

        :param versions: The versions to filter
        :type versions: iterable of :class:`Version`
        :yield: :class:`Version`


    .. method:: select(self, versions)

        Select the highest compatible version from an iterable of :class:`Version`
        objects.

        .. sourcecode:: pycon

            >>> s = Spec('>=0.1.0')
            >>> s.select([])
            None
            >>> s.select([Version('0.1.0'), Version('0.1.3'), Version('0.1.1')])
            Version('0.1.3')

        :param versions: The versions to filter
        :type versions: iterable of :class:`Version`
        :rtype: The highest compatible :class:`Version` if at least one of the
                given versions is compatible; :class:`None` otherwise.


    .. method:: __contains__(self, version)

        Alias of the :func:`match` method;
        allows the use of the ``version in speclist`` syntax::

            >>> Version('1.1.1-alpha') in Spec('>=1.1.0,<1.1.1')
            True


    .. method:: __str__(self)

        Converting a :class:`Spec` returns the initial description string::

            >>> str(Spec('>=0.1.1,!=0.1.2'))
            '>=0.1.1,!=0.1.2'

    .. method:: __hash__(self)

        Provides a hash based solely on the hash of contained specs.

        Allows using a :class:`Spec` as a dictionary key.


    .. rubric:: Class methods


    .. classmethod:: parse(self, expression, syntax='simple')

        Retrieve a :class:`BaseSpec` object tuple from a string.

        :param str requirement_string: The textual description of the specifications
        :param str syntax: The identifier of the syntax to use for parsing
        :raises: :exc:`ValueError`: if the ``requirement_string`` is invalid.
        :rtype: :class:`BaseSpec` subclass

        .. versionchanged:: 2.7
            This method used to return a tuple of :class:`SpecItem` objects.


.. class:: SimpleSpec(spec_string)

    .. versionadded:: 2.7
        Previously reachable through :class:`Spec`.

    Applies the python-semanticversion range specification:

    The main issue with representing version specifications is that the usual syntax
    does not map well onto `SemVer`_ precedence rules:

    * A specification of ``<1.3.4`` is not expected to allow ``1.3.4-rc2``, but strict `SemVer`_ comparisons allow it ;
      prereleases has the issue of excluding ``1.3.3+build3`` ;
    * It may be necessary to exclude either all variations on a patch-level release
      (``!=1.3.3``) or specifically one build-level release (``1.3.3+build.434``).


    In order to have version specification behave naturally, the rules are the following:

    * If no pre-release number was included in the specification, versions with a pre-release
      numbers are excluded from matching that specification.
    * If no build metadata was included in the specification, build metadata is ignored
      when deciding whether a version satisfies a specification.

    This means that::

        >>> Version('1.1.1-rc1') in Spec('<1.1.1')
        False
        >>> Version('1.1.1-rc1') in Spec('<1.1.1-rc4')
        True
        >>> Version('1.1.1-rc1+build4') in Spec('<=1.1.1-rc1')
        True
        >>> Version('1.1.1-rc1+build4') in Spec('==1.1.1-rc1+build2')
        False


    .. note:: python-semanticversion also accepts ``"*"`` as a version spec,
              that matches all (valid) version strings.

    .. note:: python-semanticversion supports PyPI-style `compatible release clauses`_:

              * ``~=2.2`` means "Any release between 2.2.0 and 3.0.0"
              * ``~=1.4.5`` means "Any release between 1.4.5 and 1.5.0"

    .. note:: python-semanticversion includes support for NPM-style specs:

              * ``~1.2.3`` means "Any release between 1.2.3 and 1.3.0"
              * ``^1.3.4`` means "Any release between 1.3.4 and 2.0.0"

    In order to force matches to *strictly* compare version numbers, these additional
    rules apply:

    * Setting a pre-release separator without a pre-release identifier (``<=1.1.1-``)
      forces match to take into account pre-release version::

        >>> Version('1.1.1-rc1') in Spec('<1.1.1')
        False
        >>> Version('1.1.1-rc1') in Spec('<1.1.1-')
        True

    * Setting a build metadata separator without build metadata (``<=1.1.1+``)
      forces matches "up to the build metadata"; use this to include/exclude a
      release lacking build metadata while excluding/including all other builds
      of that release::

        >>> Version('1.1.1') in Spec('==1.1.1+')
        True
        >>> Version('1.1.1+2') in Spec('==1.1.1+')
        False


    .. warning:: As stated in the `SemVer`_ specification, the ordering of build metadata is *undefined*.
                 Thus, a :class:`Spec` string can only mention build metadata to include or exclude a specific version:

                 * ``==1.1.1+b1234`` includes this specific build
                 * ``!=1.1.1+b1234`` excludes it (but would match ``1.1.1+b1235``
                 * ``<1.1.1+b1`` is invalid

.. class:: NpmSpec(spec_string)

    .. versionadded:: 2.7

    A NPM-compliant version matching engine, based on the https://docs.npmjs.com/misc/semver.html specification.

    .. code-block:: pycon

        >>> Version('0.1.2') in NpmSpec('0.1.0-alpha.2 .. 0.2.4')
        True
        >>> Version('0.1.2') in NpmSpec('>=0.1.1 <0.1.3 || 2.x')
        True
        >>> Version('2.3.4') in NpmSpec('>=0.1.1 <0.1.3 || 2.x')
        True


.. class:: Spec(spec_string)

    .. deprecated:: 2.7
        The alias from :class:`Spec` to :class:`SimpleSpec` will be removed in 3.1.

    Alias to :class:`LegacySpec`, for backwards compatibility.


.. class:: LegacySpec(spec_string)

    .. deprecated:: 2.7
        The :class:`LegacySpec` class will be removed in 3.0; use :class:`SimpleSpec` instead.

    A :class:`LegacySpec` class has the exact same behaviour as :class:`SimpleSpec`, with
    backwards-compatible features:

    It accepts version specifications passed in as separated arguments::

        >>> Spec('>=1.0.0', '<1.2.0', '!=1.1.4,!=1.1.13')
        <Spec: (
            <SpecItem: >= Version('1.0.0', partial=True)>,
            <SpecItem: < Version('1.2.0', partial=True)>,
            <SpecItem: != Version('1.1.4', partial=True)>,
            <SpecItem: != Version('1.1.13', partial=True)>,
        )>

    Its keeps a list of :class:`SpecItem` objects, based on the initial expression
    components.

    .. method:: __iter__(self)

        Returns an iterator over the contained specs::

            >>> for spec in Spec('>=0.1.1,!=0.1.2'):
            ...     print spec
            >=0.1.1
            !=0.1.2

    .. rubric:: Attributes

    .. attribute:: specs

        Tuple of :class:`SpecItem`, the included specifications.


.. class:: SpecItem(spec_string)

    .. deprecated:: 2.7
        This class will be removed in 3.0.

    .. note:: This class belong to the private python-semanticversion API.

    Stores a version specification, defined from a string::

        >>> SpecItem('>=0.1.1')
        <SpecItem: >= Version('0.1.1', partial=True)>

    This allows to test :class:`Version` objects against the :class:`SpecItem`::

        >>> SpecItem('>=0.1.1').match(Version('0.1.1-rc1'))  # pre-release satisfy conditions
        True
        >>> Version('0.1.1+build2') in SpecItem('>=0.1.1')   # build metadata is ignored when checking for precedence
        True
        >>>
        >>> # Use the '-' marker to include the pre-release component in checks
        >>> SpecItem('>=0.1.1-').match(Version('0.1.1-rc1')
        False
        >>> # Use the '+' marker to include the build metadata in checks
        >>> SpecItem('==0.1.1+').match(Version('0.1.1+b1234')
        False
        >>>


    .. rubric:: Attributes


    .. attribute:: kind

        One of :data:`KIND_LT`, :data:`KIND_LTE`, :data:`KIND_EQUAL`, :data:`KIND_GTE`,
        :data:`KIND_GT` and :data:`KIND_NEQ`.

    .. attribute:: spec

        :class:`Version` in the :class:`SpecItem` description.

        It is alway a :attr:`~Version.partial` :class:`Version`.


    .. rubric:: Class methods


    .. classmethod:: parse(cls, requirement_string)

        Retrieve a ``(kind, version)`` tuple from a string.

        :param str requirement_string: The textual description of the specification
        :raises: :exc:`ValueError`: if the ``requirement_string`` is invalid.
        :rtype: (``kind``, ``version``) tuple


    .. rubric:: Methods


    .. method:: match(self, version)

        Test whether a given :class:`Version` matches this :class:`SpecItem`::

            >>> SpecItem('>=0.1.1').match(Version('0.1.1-alpha'))
            True
            >>> SpecItem('>=0.1.1-').match(Version('0.1.1-alpha'))
            False

        :param version: The version to test against the spec
        :type version: :class:`Version`
        :rtype: ``bool``


    .. method:: __str__(self)

        Converting a :class:`SpecItem` to a string returns the initial description string::

            >>> str(SpecItem('>=0.1.1'))
            '>=0.1.1'


    .. method:: __hash__(self)

        Provides a hash based solely on the current kind and the specified version.

        Allows using a :class:`SpecItem` as a dictionary key.


    .. rubric:: Class attributes


    .. data:: KIND_LT

        The kind of 'Less than' specifications::

            >>> Version('1.0.0-alpha') in Spec('<1.0.0')
            False

    .. data:: KIND_LTE

        The kind of 'Less or equal to' specifications::

            >>> Version('1.0.0-alpha1+build999') in Spec('<=1.0.0-alpha1')
            True

    .. data:: KIND_EQUAL

        The kind of 'equal to' specifications::

            >>> Version('1.0.0+build3.3') in Spec('==1.0.0')
            True

    .. data:: KIND_GTE

        The kind of 'Greater or equal to' specifications::

            >>> Version('1.0.0') in Spec('>=1.0.0')
            True

    .. data:: KIND_GT

        The kind of 'Greater than' specifications::

            >>> Version('1.0.0+build667') in Spec('>1.0.1')
            False

    .. data:: KIND_NEQ

        The kind of 'Not equal to' specifications::

            >>> Version('1.0.1') in Spec('!=1.0.1')
            False

        The kind of 'Almost equal to' specifications



.. _SemVer: http://semver.org/
.. _`compatible release clauses`: https://www.python.org/dev/peps/pep-0440/#compatible-release