|
| 1 | +Checking for a Compatible Semver Version |
| 2 | +======================================== |
| 3 | + |
| 4 | +To check if a *change* from a semver version a to a semver version b is |
| 5 | +*compatible* according to semver rule, use :meth:`Version.is_compatible <semver.Version.is_compatible>`. |
| 6 | + |
| 7 | +The expression ``a.is_compatible(b) is True`` when either is true: |
| 8 | + |
| 9 | +* both versions are identical, or |
| 10 | +* both are releases, their majors are equal and higher than 0 and minor |
| 11 | + versions, or |
| 12 | +* both are releases, their majors are equal and higher than 0 and b's |
| 13 | + minor version is higher then a's |
| 14 | + |
| 15 | +In all other cases, the result is false. |
| 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 | + >>> b.is_compatible(a) |
| 28 | + False |
| 29 | +
|
| 30 | + # Two different minors: |
| 31 | + >>> a = Version(1, 1, 0) |
| 32 | + >>> b = Version(1, 0, 0) |
| 33 | + >>> a.is_compatible(b) |
| 34 | + False |
| 35 | + >>> b.is_compatible(a) |
| 36 | + True |
| 37 | +
|
| 38 | + # The same two majors and minors: |
| 39 | + >>> a = Version(1, 1, 1) |
| 40 | + >>> b = Version(1, 1, 0) |
| 41 | + >>> a.is_compatible(b) |
| 42 | + True |
| 43 | + >>> b.is_compatible(a) |
| 44 | + True |
| 45 | +
|
| 46 | + # Release and pre-release: |
| 47 | + >>> a = Version(1, 1, 1) |
| 48 | + >>> b = Version(1, 0, 0,'rc1') |
| 49 | + >>> a.is_compatible(b) |
| 50 | + False |
| 51 | + >>> b.is_compatible(a) |
| 52 | + False |
| 53 | +
|
| 54 | + # Different pre-releases: |
| 55 | + >>> a = Version(1, 0, 0, 'rc1') |
| 56 | + >>> b = Version(1, 0, 0, 'rc2') |
| 57 | + >>> a.is_compatible(b) |
| 58 | + False |
| 59 | + >>> b.is_compatible(a) |
| 60 | + False |
| 61 | +
|
| 62 | + # Identical pre-releases |
| 63 | + >>> a = Version(1, 0, 0,'rc1') |
| 64 | + >>> b = Version(1, 0, 0,'rc1') |
| 65 | + >>> a.is_compatible(b) |
| 66 | + True |
| 67 | +
|
| 68 | +All major zero versions are incompatible with anything but itself: |
| 69 | + |
| 70 | +.. code-block:: python |
| 71 | +
|
| 72 | + >>> Version(0,1,0).is_compatible(Version(0,1,1)) |
| 73 | + False |
| 74 | +
|
| 75 | + # Only identical versions are compatible for major zero versions: |
| 76 | + >>> Version(0,1,0).is_compatible(Version(0,1,0)) |
| 77 | + True |
0 commit comments