3.6. Checking for a Compatible Semver Version

To check if a change from a semver version a to a semver version b is compatible according to semver rule, use the method is_compatible().

The expression a.is_compatible(b) is True if one of the following statements is true:

  • both versions are equal, or

  • both majors are equal and higher than 0. The same applies for both minor parts. Both pre-releases are equal, or

  • both majors are equal and higher than 0. The minor of b’s minor version is higher then a’s. Both pre-releases are equal.

In all other cases, the result is false.

Keep in mind, the method does not check patches!

  • Two different majors:

    >>> a = Version(1, 1, 1)
    >>> b = Version(2, 0, 0)
    >>> a.is_compatible(b)
    False
    >>> b.is_compatible(a)
    False
    
  • Two different minors:

    >>> a = Version(1, 1, 0)
    >>> b = Version(1, 0, 0)
    >>> a.is_compatible(b)
    False
    >>> b.is_compatible(a)
    True
    
  • The same two majors and minors:

    >>> a = Version(1, 1, 1)
    >>> b = Version(1, 1, 0)
    >>> a.is_compatible(b)
    True
    >>> b.is_compatible(a)
    True
    
  • Release and pre-release:

    >>> a = Version(1, 1, 1)
    >>> b = Version(1, 0, 0,'rc1')
    >>> a.is_compatible(b)
    False
    >>> b.is_compatible(a)
    False
    
  • Different pre-releases:

    >>> a = Version(1, 0, 0, 'rc1')
    >>> b = Version(1, 0, 0, 'rc2')
    >>> a.is_compatible(b)
    False
    >>> b.is_compatible(a)
    False
    
  • Identical pre-releases:

    >>> a = Version(1, 0, 0,'rc1')
    >>> b = Version(1, 0, 0,'rc1')
    >>> a.is_compatible(b)
    True
    
  • All major zero versions are incompatible with anything but itself:

    >>> Version(0, 1, 0).is_compatible(Version(0, 1, 1))
    False
    
    # Only identical versions are compatible for major zero versions:
    >>> Version(0, 1, 0).is_compatible(Version(0, 1, 0))
    True