Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/numpy/lib/_version.py: 21%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

77 statements  

1"""Utility to compare (NumPy) version strings. 

2 

3The NumpyVersion class allows properly comparing numpy version strings. 

4The LooseVersion and StrictVersion classes that distutils provides don't 

5work; they don't recognize anything like alpha/beta/rc/dev versions. 

6 

7""" 

8import re 

9 

10__all__ = ['NumpyVersion'] 

11 

12 

13class NumpyVersion: 

14 """Parse and compare numpy version strings. 

15 

16 NumPy has the following versioning scheme (numbers given are examples; they 

17 can be > 9 in principle): 

18 

19 - Released version: '1.8.0', '1.8.1', etc. 

20 - Alpha: '1.8.0a1', '1.8.0a2', etc. 

21 - Beta: '1.8.0b1', '1.8.0b2', etc. 

22 - Release candidates: '1.8.0rc1', '1.8.0rc2', etc. 

23 - Development versions: '1.8.0.dev-f1234afa' (git commit hash appended) 

24 - Development versions after a1: '1.8.0a1.dev-f1234afa', 

25 '1.8.0b2.dev-f1234afa', '1.8.1rc1.dev-f1234afa', etc. 

26 - Development versions (no git hash available): '1.8.0.dev-Unknown' 

27 

28 Comparing needs to be done against a valid version string or other 

29 `NumpyVersion` instance. Note that all development versions of the same 

30 (pre-)release compare equal. 

31 

32 Parameters 

33 ---------- 

34 vstring : str 

35 NumPy version string (``np.__version__``). 

36 

37 Examples 

38 -------- 

39 >>> from numpy.lib import NumpyVersion 

40 >>> if NumpyVersion(np.__version__) < '1.7.0': 

41 ... print('skip') 

42 >>> # skip 

43 

44 >>> NumpyVersion('1.7') # raises ValueError, add ".0" 

45 Traceback (most recent call last): 

46 ... 

47 ValueError: Not a valid numpy version string 

48 

49 """ 

50 

51 __module__ = "numpy.lib" 

52 

53 def __init__(self, vstring): 

54 self.vstring = vstring 

55 ver_main = re.match(r'\d+\.\d+\.\d+', vstring) 

56 if not ver_main: 

57 raise ValueError("Not a valid numpy version string") 

58 

59 self.version = ver_main.group() 

60 self.major, self.minor, self.bugfix = [int(x) for x in 

61 self.version.split('.')] 

62 if len(vstring) == ver_main.end(): 

63 self.pre_release = 'final' 

64 else: 

65 alpha = re.match(r'a\d', vstring[ver_main.end():]) 

66 beta = re.match(r'b\d', vstring[ver_main.end():]) 

67 rc = re.match(r'rc\d', vstring[ver_main.end():]) 

68 pre_rel = [m for m in [alpha, beta, rc] if m is not None] 

69 if pre_rel: 

70 self.pre_release = pre_rel[0].group() 

71 else: 

72 self.pre_release = '' 

73 

74 self.is_devversion = bool(re.search(r'.dev', vstring)) 

75 

76 def _compare_version(self, other): 

77 """Compare major.minor.bugfix""" 

78 if self.major == other.major: 

79 if self.minor == other.minor: 

80 if self.bugfix == other.bugfix: 

81 vercmp = 0 

82 elif self.bugfix > other.bugfix: 

83 vercmp = 1 

84 else: 

85 vercmp = -1 

86 elif self.minor > other.minor: 

87 vercmp = 1 

88 else: 

89 vercmp = -1 

90 elif self.major > other.major: 

91 vercmp = 1 

92 else: 

93 vercmp = -1 

94 

95 return vercmp 

96 

97 def _compare_pre_release(self, other): 

98 """Compare alpha/beta/rc/final.""" 

99 if self.pre_release == other.pre_release: 

100 vercmp = 0 

101 elif self.pre_release == 'final': 

102 vercmp = 1 

103 elif other.pre_release == 'final': 

104 vercmp = -1 

105 elif self.pre_release > other.pre_release: 

106 vercmp = 1 

107 else: 

108 vercmp = -1 

109 

110 return vercmp 

111 

112 def _compare(self, other): 

113 if not isinstance(other, (str, NumpyVersion)): 

114 raise ValueError("Invalid object to compare with NumpyVersion.") 

115 

116 if isinstance(other, str): 

117 other = NumpyVersion(other) 

118 

119 vercmp = self._compare_version(other) 

120 if vercmp == 0: 

121 # Same x.y.z version, check for alpha/beta/rc 

122 vercmp = self._compare_pre_release(other) 

123 if vercmp == 0: 

124 # Same version and same pre-release, check if dev version 

125 if self.is_devversion is other.is_devversion: 

126 vercmp = 0 

127 elif self.is_devversion: 

128 vercmp = -1 

129 else: 

130 vercmp = 1 

131 

132 return vercmp 

133 

134 def __lt__(self, other): 

135 return self._compare(other) < 0 

136 

137 def __le__(self, other): 

138 return self._compare(other) <= 0 

139 

140 def __eq__(self, other): 

141 return self._compare(other) == 0 

142 

143 def __ne__(self, other): 

144 return self._compare(other) != 0 

145 

146 def __gt__(self, other): 

147 return self._compare(other) > 0 

148 

149 def __ge__(self, other): 

150 return self._compare(other) >= 0 

151 

152 def __repr__(self): 

153 return f"NumpyVersion({self.vstring})"