-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
347 lines (263 loc) · 9.55 KB
/
version.py
File metadata and controls
347 lines (263 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from __future__ import annotations
import re
from collections.abc import Callable
from contextlib import suppress
from dataclasses import dataclass, field, replace
from functools import total_ordering
from typing import Any, Self, assert_never, overload, override
from utilities.constants import Sentinel
from utilities.types import MaybeCallable, MaybeStr
type Version2Like = MaybeStr[Version2]
type Version3Like = MaybeStr[Version3]
type Version2Or3 = Version2 | Version3
type MaybeCallableVersion3Like = MaybeCallable[Version3Like]
##
_PARSE_VERSION2_PATTERN = re.compile(r"^(\d+)\.(\d+)(?!\.\d)(?:-(\w+))?")
@dataclass(repr=False, frozen=True, slots=True)
@total_ordering
class Version2:
"""A version identifier."""
major: int = 0
minor: int = 1
suffix: str | None = field(default=None, kw_only=True)
def __post_init__(self) -> None:
if (self.major == 0) and (self.minor == 0):
raise _Version2ZeroError(major=self.major, minor=self.minor)
if self.major < 0:
raise _Version2NegativeMajorVersionError(major=self.major)
if self.minor < 0:
raise _Version2NegativeMinorVersionError(minor=self.minor)
if (self.suffix is not None) and (len(self.suffix) == 0):
raise _Version2EmptySuffixError(suffix=self.suffix)
def __le__(self, other: Any, /) -> bool:
if not isinstance(other, type(self)):
return NotImplemented
self_as_tuple = (
self.major,
self.minor,
"" if self.suffix is None else self.suffix,
)
other_as_tuple = (
other.major,
other.minor,
"" if other.suffix is None else other.suffix,
)
return self_as_tuple <= other_as_tuple
@override
def __repr__(self) -> str:
version = f"{self.major}.{self.minor}"
if self.suffix is not None:
version = f"{version}-{self.suffix}"
return version
@classmethod
def parse(cls, text: str, /) -> Self:
"""Parse a string into a Version2 object."""
try:
((major, minor, suffix),) = _PARSE_VERSION2_PATTERN.findall(text)
except ValueError:
raise _Version2ParseError(text=text) from None
return cls(int(major), int(minor), suffix=None if suffix == "" else suffix)
def bump_major(self) -> Self:
"""Bump the major component."""
return type(self)(self.major + 1, 0)
def bump_minor(self) -> Self:
"""Bump the minor component."""
return type(self)(self.major, self.minor + 1)
def version3(self, *, patch: int = 0) -> Version3:
"""Convert to a Version3 object."""
return Version3(self.major, self.minor, patch, suffix=self.suffix)
def with_suffix(self, *, suffix: str | None = None) -> Self:
"""Replace the suffix."""
return replace(self, suffix=suffix)
@dataclass(kw_only=True, slots=True)
class Version2Error(Exception): ...
@dataclass(kw_only=True, slots=True)
class _Version2ZeroError(Version2Error):
major: int
minor: int
@override
def __str__(self) -> str:
return f"Version must be greater than zero; got {self.major}.{self.minor}"
@dataclass(kw_only=True, slots=True)
class _Version2NegativeMajorVersionError(Version2Error):
major: int
@override
def __str__(self) -> str:
return f"Major version must be non-negative; got {self.major}"
@dataclass(kw_only=True, slots=True)
class _Version2NegativeMinorVersionError(Version2Error):
minor: int
@override
def __str__(self) -> str:
return f"Minor version must be non-negative; got {self.minor}"
@dataclass(kw_only=True, slots=True)
class _Version2EmptySuffixError(Version2Error):
suffix: str
@override
def __str__(self) -> str:
return f"Suffix must be non-empty; got {self.suffix!r}"
@dataclass(kw_only=True, slots=True)
class _Version2ParseError(Version2Error):
text: str
@override
def __str__(self) -> str:
return f"Unable to parse version; got {self.text!r}"
##
_PARSE_VERSION3_PATTERN = re.compile(r"^(\d+)\.(\d+)\.(\d+)(?:-(\w+))?")
@dataclass(repr=False, frozen=True, slots=True)
@total_ordering
class Version3:
"""A version identifier."""
major: int = 0
minor: int = 0
patch: int = 1
suffix: str | None = field(default=None, kw_only=True)
def __post_init__(self) -> None:
if (self.major == 0) and (self.minor == 0) and (self.patch == 0):
raise _Version3ZeroError(
major=self.major, minor=self.minor, patch=self.patch
)
if self.major < 0:
raise _Version3NegativeMajorVersionError(major=self.major)
if self.minor < 0:
raise _Version3NegativeMinorVersionError(minor=self.minor)
if self.patch < 0:
raise _Version3NegativePatchVersionError(patch=self.patch)
if (self.suffix is not None) and (len(self.suffix) == 0):
raise _Version3EmptySuffixError(suffix=self.suffix)
def __le__(self, other: Any, /) -> bool:
if not isinstance(other, type(self)):
return NotImplemented
self_as_tuple = (
self.major,
self.minor,
self.patch,
"" if self.suffix is None else self.suffix,
)
other_as_tuple = (
other.major,
other.minor,
other.patch,
"" if other.suffix is None else other.suffix,
)
return self_as_tuple <= other_as_tuple
@override
def __repr__(self) -> str:
version = f"{self.major}.{self.minor}.{self.patch}"
if self.suffix is not None:
version = f"{version}-{self.suffix}"
return version
@classmethod
def parse(cls, text: str, /) -> Self:
"""Parse a string into a Version3 object."""
try:
((major, minor, patch, suffix),) = _PARSE_VERSION3_PATTERN.findall(text)
except ValueError:
raise _Version3ParseError(text=text) from None
return cls(
int(major), int(minor), int(patch), suffix=None if suffix == "" else suffix
)
def bump_major(self) -> Self:
"""Bump the major component."""
return type(self)(self.major + 1, 0, 0)
def bump_minor(self) -> Self:
"""Bump the minor component."""
return type(self)(self.major, self.minor + 1, 0)
def bump_patch(self) -> Self:
"""Bump the patch component."""
return type(self)(self.major, self.minor, self.patch + 1)
@property
def version2(self) -> Version2:
"""Return the major/minor components only."""
return Version2(self.major, self.minor, suffix=self.suffix)
def with_suffix(self, *, suffix: str | None = None) -> Self:
"""Replace the suffix."""
return replace(self, suffix=suffix)
@dataclass(kw_only=True, slots=True)
class Version3Error(Exception): ...
@dataclass(kw_only=True, slots=True)
class _Version3ZeroError(Version3Error):
major: int
minor: int
patch: int
@override
def __str__(self) -> str:
return f"Version must be greater than zero; got {self.major}.{self.minor}.{self.patch}"
@dataclass(kw_only=True, slots=True)
class _Version3NegativeMajorVersionError(Version3Error):
major: int
@override
def __str__(self) -> str:
return f"Major version must be non-negative; got {self.major}"
@dataclass(kw_only=True, slots=True)
class _Version3NegativeMinorVersionError(Version3Error):
minor: int
@override
def __str__(self) -> str:
return f"Minor version must be non-negative; got {self.minor}"
@dataclass(kw_only=True, slots=True)
class _Version3NegativePatchVersionError(Version3Error):
patch: int
@override
def __str__(self) -> str:
return f"Patch version must be non-negative; got {self.patch}"
@dataclass(kw_only=True, slots=True)
class _Version3EmptySuffixError(Version3Error):
suffix: str
@override
def __str__(self) -> str:
return f"Suffix must be non-empty; got {self.suffix!r}"
@dataclass(kw_only=True, slots=True)
class _Version3ParseError(Version3Error):
text: str
@override
def __str__(self) -> str:
return f"Unable to parse version; got {self.text!r}"
##
def parse_version_2_or_3(text: str, /) -> Version2Or3:
"""Parse a string into a Version2 or Version3 object."""
with suppress(_Version2ParseError):
return Version2.parse(text)
with suppress(_Version3ParseError):
return Version3.parse(text)
raise ParseVersion2Or3Error(text=text)
@dataclass(kw_only=True, slots=True)
class ParseVersion2Or3Error(Exception):
text: str
@override
def __str__(self) -> str:
return f"Unable to parse Version2 or Version3; got {self.text!r}"
##
@overload
def to_version3(version: MaybeCallableVersion3Like, /) -> Version3: ...
@overload
def to_version3(version: None, /) -> None: ...
@overload
def to_version3(version: Sentinel, /) -> Sentinel: ...
def to_version3(
version: MaybeCallableVersion3Like | None | Sentinel, /
) -> Version3 | None | Sentinel:
"""Convert to a version."""
match version:
case Version3() | None | Sentinel():
return version
case str():
return Version3.parse(version)
case Callable() as func:
return to_version3(func())
case never:
assert_never(never)
##
__all__ = [
"MaybeCallableVersion3Like",
"ParseVersion2Or3Error",
"Version2",
"Version2Error",
"Version2Like",
"Version2Or3",
"Version3",
"Version3Error",
"Version3Like",
"parse_version_2_or_3",
"to_version3",
]