|
| 1 | +Checking for a Compatible Semver Version |
| 2 | +======================================== |
| 3 | + |
| 4 | +If you need to check if a semver version is compatible |
| 5 | +with another semver version, use :meth:`Version.is_compatible <semver.Version.is_compatible>`. |
| 6 | + |
| 7 | +A version `a` is compatible with another version `b` if: |
| 8 | + |
| 9 | +* Both types are of type :class:`Version <semver.version.Version>`. |
| 10 | +* The two majors parts equal. |
| 11 | +* The two major parts are equal and the minor of ``a`` is lower |
| 12 | + than the minor of ``b``. |
| 13 | +* Both pre-releases are present and equal. |
| 14 | + |
| 15 | +In all other cases, it's incompatible. |
| 16 | + |
| 17 | +Keep in mind, the method *does not* check patches! |
| 18 | + |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + # Two different majors: |
| 23 | + >>> a = Version(1, 1, 1) # This is "A" |
| 24 | + >>> b = Version(2, 0, 0) # This is "B" |
| 25 | + >>> a.is_compatible(b) |
| 26 | + False |
| 27 | +
|
| 28 | + # The same two majors and minors: |
| 29 | + >>> b = Version(1, 1, 0) # This is another "B" |
| 30 | + >>> a.is_compatible(b) |
| 31 | + True |
| 32 | +
|
| 33 | + # The same two majors, but B.minor < A.minor: |
| 34 | + >>> b = Version(1, 0, 0) # This is another "B" |
| 35 | + >>> a.is_compatible(b) |
| 36 | + False |
| 37 | +
|
| 38 | + # Checking compatibility between release and pre-release: |
| 39 | + >>> b = Version(1,0,0,'rc1.') |
| 40 | + >>> a.is_compatible(b) |
| 41 | + False |
| 42 | +
|
| 43 | + # But pre-release and release: |
| 44 | + >>> b.is_compatible(a) |
| 45 | + False |
| 46 | +
|
| 47 | +All major zero versions are incompatible with anything but itself: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + >>> Version(0,1,0).is_compatible(Version(0,1,1)) |
| 52 | + False |
| 53 | +
|
| 54 | + # Only identical versions are compatible for major zero versions: |
| 55 | + >>> Version(0,1,0).is_compatible(Version(0,1,0)) |
| 56 | + True |
0 commit comments