Skip to content

Commit 306da07

Browse files
committed
Update __gt__ logic to leverage tuple comparison
1 parent a11ee11 commit 306da07

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

cassandra/util.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,18 +1724,21 @@ def __init__(self, version):
17241724
try:
17251725
self.prerelease = self._cleanup_str(match[5])
17261726
except:
1727-
self.prerelease = 0
1727+
self.prerelease = ""
1728+
1729+
# This is used in a few places below so let's just build it now
1730+
self._tuple = (self.major, self.minor, self.patch, self.build, self.prerelease)
17281731

17291732
# Trim off the leading '.' characters and convert the discovered value to an integer
17301733
def _cleanup_int(self, instr):
17311734
return int(instr[1:]) if instr else 0
17321735

17331736
# Trim off the leading '.' or '~' characters and just return the string directly
17341737
def _cleanup_str(self, instr):
1735-
return instr[1:] if instr else 0
1738+
return instr[1:] if instr else ""
17361739

17371740
def __hash__(self):
1738-
return hash((self.major, self.minor, self.patch, self.build, self.prerelease))
1741+
return hash(self._tuple)
17391742

17401743
def __repr__(self):
17411744
version_string = "Version({0}, {1}, {2}".format(self.major, self.minor, self.patch)
@@ -1750,29 +1753,24 @@ def __repr__(self):
17501753
def __str__(self):
17511754
return self._version
17521755

1756+
# Methods below leverage the fact that Tuples are compared position by position from left to right
17531757
def __eq__(self, other):
17541758
if not isinstance(other, Version):
17551759
return NotImplemented
17561760

1757-
return (self.major == other.major and
1758-
self.minor == other.minor and
1759-
self.patch == other.patch and
1760-
self.build == other.build and
1761-
self.prerelease == other.prerelease
1762-
)
1761+
return self._tuple == other._tuple
17631762

17641763
def __gt__(self, other):
17651764
if not isinstance(other, Version):
17661765
return NotImplemented
17671766

1768-
if self.major != other.major:
1769-
return self.major > other.major
1770-
elif self.minor != other.minor:
1771-
return self.minor > other.minor
1772-
elif self.patch != other.patch:
1773-
return self.patch > other.patch
1774-
elif self.build != other.build:
1775-
return self.build > other.build
1767+
# We start by comparing the first four fields directly
1768+
self_tuple = self._tuple[:4]
1769+
other_tuple = (other.major, other.minor, other.patch, other.build)
1770+
if self_tuple != other_tuple:
1771+
return self_tuple > other_tuple
1772+
# If we're still around we have to check prereleases... prereleases always come before
1773+
# the corresponding version
17761774
elif self.prerelease and not other.prerelease:
17771775
return False
17781776
elif other.prerelease and not self.prerelease:

tests/unit/test_util_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ def test_version_parsing(self):
211211
versions = [
212212
# Test cases here adapted from the Java driver cases
213213
# (https://github.com/apache/cassandra-java-driver/blob/4.19.2/core/src/test/java/com/datastax/oss/driver/api/core/VersionTest.java)
214-
('1.2.19', (1, 2, 19, 0, 0)),
215-
('1.2', (1, 2, 0, 0, 0)),
214+
('1.2.19', (1, 2, 19, 0, "")),
215+
('1.2', (1, 2, 0, 0, "")),
216216
('1.2-beta1-SNAPSHOT', (1, 2, 0, 0, 'beta1-SNAPSHOT')),
217217
('1.2~beta1-SNAPSHOT', (1, 2, 0, 0, 'beta1-SNAPSHOT')),
218218
('1.2.19.2-SNAPSHOT', (1, 2, 19, 2, 'SNAPSHOT')),

0 commit comments

Comments
 (0)