Coverage for value_object_pattern/usables/internet/semantic_version_value_object.py: 62.07%

27 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-11-21 14:31 +0000

1""" 

2Provide a value object for normal semantic versions. 

3""" 

4 

5from re import Pattern, compile as re_compile 1abcdefghij

6from typing import NoReturn, Self 1abcdefghij

7 

8from value_object_pattern.decorators import validation 1abcdefghij

9from value_object_pattern.usables import NotEmptyStringValueObject, TrimmedStringValueObject 1abcdefghij

10 

11 

12class SemanticVersionValueObject(NotEmptyStringValueObject, TrimmedStringValueObject): 1abcdefghij

13 """ 

14 Validate a normal semantic version in `major.minor.patch` form. Each component is a non-negative ASCII integer 

15 without leading zeros. Pre-release identifiers, build metadata, version prefixes, and surrounding whitespace are 

16 not accepted. The original string is preserved, while `major`, `minor`, and `patch` expose integer components. 

17 Increment methods return a new instance of the same subclass and preserve error metadata. Increasing minor resets 

18 patch; increasing major resets both minor and patch. 

19 

20 References: 

21 Semantic Versioning 2.0.0: https://semver.org/spec/v2.0.0.html 

22 

23 Example: 

24 ```python 

25 from value_object_pattern.usables.internet import SemanticVersionValueObject 

26 

27 version = SemanticVersionValueObject(value='1.2.3') 

28 print(version.major) 

29 # >>> 1 

30 print(version.minor) 

31 # >>> 2 

32 print(version.patch) 

33 # >>> 3 

34 ``` 

35 """ 

36 

37 _VALIDATION_REGEX: Pattern[str] = re_compile(pattern=r'(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)') 1abcdefghij

38 

39 @property 1abcdefghij

40 def major(self) -> int: 1abcdefghij

41 """ 

42 Return the major version component as an integer. 

43 

44 Returns: 

45 int: The major version component. 

46 

47 Example: 

48 ```python 

49 from value_object_pattern.usables.internet import SemanticVersionValueObject 

50 

51 version = SemanticVersionValueObject(value='1.2.3') 

52 print(version.major) 

53 # >>> 1 

54 ``` 

55 """ 

56 return int(self.value.split('.')[0]) 

57 

58 @property 1abcdefghij

59 def minor(self) -> int: 1abcdefghij

60 """ 

61 Return the minor version component as an integer. 

62 

63 Returns: 

64 int: The minor version component. 

65 

66 Example: 

67 ```python 

68 from value_object_pattern.usables.internet import SemanticVersionValueObject 

69 

70 version = SemanticVersionValueObject(value='1.2.3') 

71 print(version.minor) 

72 # >>> 2 

73 ``` 

74 """ 

75 return int(self.value.split('.')[1]) 

76 

77 @property 1abcdefghij

78 def patch(self) -> int: 1abcdefghij

79 """ 

80 Return the patch version component as an integer. 

81 

82 Returns: 

83 int: The patch version component. 

84 

85 Example: 

86 ```python 

87 from value_object_pattern.usables.internet import SemanticVersionValueObject 

88 

89 version = SemanticVersionValueObject(value='1.2.3') 

90 print(version.patch) 

91 # >>> 3 

92 ``` 

93 """ 

94 return int(self.value.split('.')[2]) 

95 

96 def increase_patch(self) -> Self: 1abcdefghij

97 """ 

98 Return a new version with patch increased by one, preserving major and minor. 

99 

100 Returns: 

101 Self: A new instance of the same subclass with the patch version increased by one. 

102 

103 Example: 

104 ```python 

105 from value_object_pattern.usables.internet import SemanticVersionValueObject 

106 

107 version = SemanticVersionValueObject(value='1.2.3') 

108 new_version = version.increase_patch() 

109 print(new_version.value) 

110 # >>> '1.2.4' 

111 ``` 

112 """ 

113 return self.__class__( 

114 value=f'{self.major}.{self.minor}.{self.patch + 1}', 

115 title=self.title, 

116 parameter=self.parameter, 

117 ) 

118 

119 def increase_minor(self) -> Self: 1abcdefghij

120 """ 

121 Return a new version with minor increased by one and patch reset to zero. 

122 

123 Returns: 

124 Self: A new instance of the same subclass with the minor version increased by one and patch reset to zero. 

125 

126 Example: 

127 ```python 

128 from value_object_pattern.usables.internet import SemanticVersionValueObject 

129 

130 version = SemanticVersionValueObject(value='1.2.3') 

131 new_version = version.increase_minor() 

132 print(new_version.value) 

133 # >>> '1.3.0' 

134 ``` 

135 """ 

136 return self.__class__( 

137 value=f'{self.major}.{self.minor + 1}.0', 

138 title=self.title, 

139 parameter=self.parameter, 

140 ) 

141 

142 def increase_major(self) -> Self: 1abcdefghij

143 """ 

144 Return a new version with major increased by one and minor and patch reset to zero. 

145 

146 Returns: 

147 Self: A new instance of the same subclass with the major version increased by one and minor and patch reset 

148 to zero. 

149 

150 Example: 

151 ```python 

152 from value_object_pattern.usables.internet import SemanticVersionValueObject 

153 

154 version = SemanticVersionValueObject(value='1.2.3') 

155 new_version = version.increase_major() 

156 print(new_version.value) 

157 # >>> '2.0.0' 

158 ``` 

159 """ 

160 return self.__class__( 

161 value=f'{self.major + 1}.0.0', 

162 title=self.title, 

163 parameter=self.parameter, 

164 ) 

165 

166 @validation(order=0) 1abcdefghij

167 def _ensure_value_is_semantic_version(self, value: str) -> None: 1abcdefghij

168 """ 

169 Ensure the value is a normal semantic version. 

170 

171 Args: 

172 value (str): The provided version string. 

173 

174 Raises: 

175 ValueError: If the value does not use the `major.minor.patch` format. 

176 """ 

177 if not self._VALIDATION_REGEX.fullmatch(string=value): 

178 self._raise_value_is_not_semantic_version(value=value) 

179 

180 def _raise_value_is_not_semantic_version(self, value: str) -> NoReturn: 1abcdefghij

181 """ 

182 Raise an error for an invalid normal semantic version. 

183 

184 Args: 

185 value (str): The invalid version string. 

186 

187 Raises: 

188 ValueError: Always raised because the value is not a normal semantic version. 

189 """ 

190 raise ValueError(f'SemanticVersionValueObject value <<<{value}>>> must use major.minor.patch format with non-negative ASCII integers and no leading zeros.') # noqa: E501 # fmt: skip