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