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 17cfd1b commit 0e94e0c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
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
13 changes: 9 additions & 4 deletions semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

from functools import wraps

__version__ = '2.8.1'

__version__ = '2.8.2'
__author__ = 'Kostiantyn Rybnikov'
__author_email__ = 'k-bx@k-bx.com'
__maintainer__ = 'Sebastien Celles'
Expand Down Expand Up @@ -78,8 +79,10 @@ def comparator(operator):
""" Wrap a VersionInfo binary op method in a type-check """
@wraps(operator)
def wrapper(self, other):
if not isinstance(other, (VersionInfo, dict)):
return NotImplemented
comparable_types = (VersionInfo, dict, tuple)
if not isinstance(other, comparable_types):
raise TypeError("other type %r must be in %r"
% (type(other), comparable_types))
return operator(self, other)
return wrapper

Expand All @@ -95,7 +98,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 @@ -215,6 +218,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
40 changes: 40 additions & 0 deletions test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,46 @@ def test_should_compare_version_dictionaries():
assert not(v1 == v4)


def test_should_compare_version_tuples():
v0 = VersionInfo(major=0, minor=4, patch=5,
prerelease='pre.2', build='build.4')
v1 = VersionInfo(major=3, minor=4, patch=5,
prerelease='pre.2', build='build.4')
for t in ((1, 0, 0), (1, 0), (1,), (1, 0, 0, 'pre.2'),
(1, 0, 0, 'pre.2', 'build.4')):
assert v0 < t
assert v0 <= t
assert v0 != t
assert not v0 == t
assert v1 > t
assert v1 >= t
# Symmetric
assert t > v0
assert t >= v0
assert t < v1
assert t <= v1
assert t != v0
assert not t == v0


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(TypeError):
v1 > "1.0.0"
with pytest.raises(TypeError):
"1.0.0" > v1


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(TypeError):
v1 > 1
with pytest.raises(TypeError):
1 > v1


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 0e94e0c

Please sign in to comment.