Fix SemanticVersion comparison (#69395)

* Fix SemanticVersion comparison.

* Complete tests for _Alpha and _Numeric comparators.

* Linting, and add comment.
pull/69436/head
Felix Fontein 5 years ago committed by GitHub
parent de59b17c7f
commit 5b9418c06c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -72,14 +72,14 @@ class _Alpha:
raise ValueError raise ValueError
def __gt__(self, other):
return not self.__lt__(other)
def __le__(self, other): def __le__(self, other):
return self.__lt__(other) or self.__eq__(other) return self.__lt__(other) or self.__eq__(other)
def __gt__(self, other):
return not self.__le__(other)
def __ge__(self, other): def __ge__(self, other):
return self.__gt__(other) or self.__eq__(other) return not self.__lt__(other)
class _Numeric: class _Numeric:
@ -115,14 +115,14 @@ class _Numeric:
raise ValueError raise ValueError
def __gt__(self, other):
return not self.__lt__(other)
def __le__(self, other): def __le__(self, other):
return self.__lt__(other) or self.__eq__(other) return self.__lt__(other) or self.__eq__(other)
def __gt__(self, other):
return not self.__le__(other)
def __ge__(self, other): def __ge__(self, other):
return self.__gt__(other) or self.__eq__(other) return not self.__lt__(other)
class SemanticVersion(Version): class SemanticVersion(Version):

@ -268,6 +268,31 @@ def test_alpha():
assert _Alpha('b') >= _Alpha('a') assert _Alpha('b') >= _Alpha('a')
assert _Alpha('b') >= _Alpha('b') assert _Alpha('b') >= _Alpha('b')
# The following 3*6 tests check that all comparison operators perform
# as expected. DO NOT remove any of them, or reformulate them (to remove
# the explicit `not`)!
assert _Alpha('a') == _Alpha('a')
assert not _Alpha('a') != _Alpha('a') # pylint: disable=unneeded-not
assert not _Alpha('a') < _Alpha('a') # pylint: disable=unneeded-not
assert _Alpha('a') <= _Alpha('a')
assert not _Alpha('a') > _Alpha('a') # pylint: disable=unneeded-not
assert _Alpha('a') >= _Alpha('a')
assert not _Alpha('a') == _Alpha('b') # pylint: disable=unneeded-not
assert _Alpha('a') != _Alpha('b')
assert _Alpha('a') < _Alpha('b')
assert _Alpha('a') <= _Alpha('b')
assert not _Alpha('a') > _Alpha('b') # pylint: disable=unneeded-not
assert not _Alpha('a') >= _Alpha('b') # pylint: disable=unneeded-not
assert not _Alpha('b') == _Alpha('a') # pylint: disable=unneeded-not
assert _Alpha('b') != _Alpha('a')
assert not _Alpha('b') < _Alpha('a') # pylint: disable=unneeded-not
assert not _Alpha('b') <= _Alpha('a') # pylint: disable=unneeded-not
assert _Alpha('b') > _Alpha('a')
assert _Alpha('b') >= _Alpha('a')
def test_numeric(): def test_numeric():
assert _Numeric(1) == _Numeric(1) assert _Numeric(1) == _Numeric(1)
@ -283,3 +308,28 @@ def test_numeric():
assert _Numeric(1) <= _Numeric(2) assert _Numeric(1) <= _Numeric(2)
assert _Numeric(2) >= _Numeric(1) assert _Numeric(2) >= _Numeric(1)
assert _Numeric(2) >= _Numeric(2) assert _Numeric(2) >= _Numeric(2)
# The following 3*6 tests check that all comparison operators perform
# as expected. DO NOT remove any of them, or reformulate them (to remove
# the explicit `not`)!
assert _Numeric(1) == _Numeric(1)
assert not _Numeric(1) != _Numeric(1) # pylint: disable=unneeded-not
assert not _Numeric(1) < _Numeric(1) # pylint: disable=unneeded-not
assert _Numeric(1) <= _Numeric(1)
assert not _Numeric(1) > _Numeric(1) # pylint: disable=unneeded-not
assert _Numeric(1) >= _Numeric(1)
assert not _Numeric(1) == _Numeric(2) # pylint: disable=unneeded-not
assert _Numeric(1) != _Numeric(2)
assert _Numeric(1) < _Numeric(2)
assert _Numeric(1) <= _Numeric(2)
assert not _Numeric(1) > _Numeric(2) # pylint: disable=unneeded-not
assert not _Numeric(1) >= _Numeric(2) # pylint: disable=unneeded-not
assert not _Numeric(2) == _Numeric(1) # pylint: disable=unneeded-not
assert _Numeric(2) != _Numeric(1)
assert not _Numeric(2) < _Numeric(1) # pylint: disable=unneeded-not
assert not _Numeric(2) <= _Numeric(1) # pylint: disable=unneeded-not
assert _Numeric(2) > _Numeric(1)
assert _Numeric(2) >= _Numeric(1)

Loading…
Cancel
Save