rbarrois / python-semanticversion

Semantic version comparison for Python (see http://semver.org/)
BSD 2-Clause "Simplified" License
281 stars 74 forks source link

Check version bump severity or implement subtraction #102

Open ewen-lbh opened 4 years ago

ewen-lbh commented 4 years ago

I needed to check for a breaking change in my program and made the following utility function:

from semantic_version import Version

def is_change_breaking(from_version: Version, to_version: Version) -> bool:
    return from_version.major != to_version.major

And I thought that this would be an easy addition to the library, although using operators directly seems more "pure":

class Version(idk from what it inherits):
    ...
    def is_change_major(self, other: 'Version') -> bool:
        return self.major != other.major

    def is_change_minor(self, other: 'Version') -> bool:
        return self.minor != other.minor

    def is_change_patch(self, other: 'Version') -> bool:
        return self.patch != other.patch
    ...

And I also thought about just implementing subtraction, so you could check the difference in the number of patches, minor and/or major bumps:

>>> from semantic_version import Version
>>> Version('1.0.8') - Version('4.5.8')
VersionDifference(major=3, minor=-5, patch=0)

So that checking for breaking changes would be:

...
if (upgrade_from - upgrade_to).major:
    print("Warning: breaking change(s) ahead!")
...

I haven't though about how to handle prereleases/build versions but I think it may be a nice addition (especially the second solution, which feels more "pythonic" to me) but of course using v_a.major != v_b.major is plenty enough too.

Just wanted to throw this idea here :)