Skip to content

Commit

Permalink
Fix comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
scls19fr authored and Flamefire committed May 29, 2019
1 parent 2faf26c commit fecfb67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Python SemVer library
All notable changes to this code base will be documented in this file,
in every released version.

Version 2.8.x
=============
:Released: 20yy-mm-dd
:Maintainer: Sébastien Celles <s.celles@gmail.com>

* Issue #102 (PR #...). Fix comparison between VersionInfo and tuple
* Issue #103 (PR #...). Disallow comparison between VersionInfo and string (and int)

Version 2.8.1
=============
:Released: 2018-07-09
Expand Down
17 changes: 16 additions & 1 deletion semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class VersionInfo(object):
"""
__slots__ = ('_major', '_minor', '_patch', '_prerelease', '_build')

def __init__(self, major, minor, patch, prerelease=None, build=None):
def __init__(self, major, minor=0, patch=0, prerelease=None, build=None):
self._major = major
self._minor = minor
self._patch = patch
Expand Down Expand Up @@ -163,26 +163,32 @@ def __iter__(self):

@comparator
def __eq__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) == 0

@comparator
def __ne__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) != 0

@comparator
def __lt__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) < 0

@comparator
def __le__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) <= 0

@comparator
def __gt__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) > 0

@comparator
def __ge__(self, other):
_ensure_is_comparable(other)
return _compare_by_keys(self._asdict(), _to_dict(other)) >= 0

def __repr__(self):
Expand Down Expand Up @@ -215,6 +221,8 @@ def parse(version):
def _to_dict(obj):
if isinstance(obj, VersionInfo):
return obj._asdict()
elif isinstance(obj, tuple):
return VersionInfo(*obj)._asdict()
return obj


Expand Down Expand Up @@ -292,6 +300,13 @@ def _compare_by_keys(d1, d2):
return rccmp


def _ensure_is_comparable(other):
comparable_types = (VersionInfo, dict, tuple)
if not isinstance(other, comparable_types):
raise NotImplementedError("other type %r must be in %r"
% (type(other), comparable_types))


def compare(ver1, ver2):
"""Compare two versions
Expand Down
24 changes: 24 additions & 0 deletions test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,30 @@ def test_should_compare_version_dictionaries():
assert not(v1 == v4)


def test_should_compare_version_tuples():
v1 = VersionInfo(major=3, minor=4, patch=5,
prerelease='pre.2', build='build.4')
assert v1 > (1, 0, 0)
assert v1 > (1, 0)
assert v1 > (1,)
assert v1 > (1, 0, 0, 'pre.2')
assert v1 > (1, 0, 0, 'pre.2', 'build.4')


def test_should_not_allow_to_compare_version_with_string():
v1 = VersionInfo(major=3, minor=4, patch=5,
prerelease='pre.2', build='build.4')
with pytest.raises(NotImplementedError) as e_info: # noqa
v1 > "1.0.0"


def test_should_not_allow_to_compare_version_with_int():
v1 = VersionInfo(major=3, minor=4, patch=5,
prerelease='pre.2', build='build.4')
with pytest.raises(NotImplementedError) as e_info: # noqa
v1 > 1


def test_should_compare_prerelease_with_numbers_and_letters():
v1 = VersionInfo(major=1, minor=9, patch=1, prerelease='1unms', build=None)
v2 = VersionInfo(major=1, minor=9, patch=1, prerelease=None, build='1asd')
Expand Down

0 comments on commit fecfb67

Please sign in to comment.