@@ -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 :
0 commit comments