This class abstracts handling of a project's versions. A :class:`Version` instance is comparison aware and can be compared and sorted using the standard Python interfaces. >>> v1 = Version("1.0a5") >>> v2 = Version("1.0") >>> v1 >>> v2 <Version('1
| 157 | |
| 158 | |
| 159 | class Version(_BaseVersion): |
| 160 | """This class abstracts handling of a project's versions. |
| 161 | |
| 162 | A :class:`Version` instance is comparison aware and can be compared and |
| 163 | sorted using the standard Python interfaces. |
| 164 | |
| 165 | >>> v1 = Version("1.0a5") |
| 166 | >>> v2 = Version("1.0") |
| 167 | >>> v1 |
| 168 | <Version('1.0a5')> |
| 169 | >>> v2 |
| 170 | <Version('1.0')> |
| 171 | >>> v1 < v2 |
| 172 | True |
| 173 | >>> v1 == v2 |
| 174 | False |
| 175 | >>> v1 > v2 |
| 176 | False |
| 177 | >>> v1 >= v2 |
| 178 | False |
| 179 | >>> v1 <= v2 |
| 180 | True |
| 181 | """ |
| 182 | |
| 183 | _regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE) |
| 184 | _key: CmpKey |
| 185 | |
| 186 | def __init__(self, version: str) -> None: |
| 187 | """Initialize a Version object. |
| 188 | |
| 189 | :param version: |
| 190 | The string representation of a version which will be parsed and normalized |
| 191 | before use. |
| 192 | :raises InvalidVersion: |
| 193 | If the ``version`` does not conform to PEP 440 in any way then this |
| 194 | exception will be raised. |
| 195 | """ |
| 196 | |
| 197 | # Validate the version and parse it into pieces |
| 198 | match = self._regex.search(version) |
| 199 | if not match: |
| 200 | raise InvalidVersion(f"Invalid version: '{version}'") |
| 201 | |
| 202 | # Store the parsed out pieces of the version |
| 203 | self._version = _Version( |
| 204 | epoch=int(match.group("epoch")) if match.group("epoch") else 0, |
| 205 | release=tuple(int(i) for i in match.group("release").split(".")), |
| 206 | pre=_parse_letter_version(match.group("pre_l"), match.group("pre_n")), |
| 207 | post=_parse_letter_version( |
| 208 | match.group("post_l"), match.group("post_n1") or match.group("post_n2") |
| 209 | ), |
| 210 | dev=_parse_letter_version(match.group("dev_l"), match.group("dev_n")), |
| 211 | local=_parse_local_version(match.group("local")), |
| 212 | ) |
| 213 | |
| 214 | # Generate a key which will be used for sorting |
| 215 | self._key = _cmpkey( |
| 216 | self._version.epoch, |
no test coverage detected