-
Notifications
You must be signed in to change notification settings - Fork 242
/
version.py
188 lines (146 loc) · 5.16 KB
/
version.py
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
import dataclasses
from typing import TYPE_CHECKING
from typing import Optional
from typing import Tuple
from typing import Union
from poetry.core.semver.empty_constraint import EmptyConstraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.semver.version_union import VersionUnion
from poetry.core.version.pep440 import Release
from poetry.core.version.pep440 import ReleaseTag
from poetry.core.version.pep440.version import PEP440Version
if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.version.pep440 import LocalSegmentType
@dataclasses.dataclass(frozen=True)
class Version(PEP440Version, VersionRangeConstraint):
"""
A parsed semantic version number.
"""
@property
def precision(self) -> int:
return self.release.precision
@property
def stable(self) -> "Version":
if self.is_stable():
return self
return self.next_patch()
def next_breaking(self) -> "Version":
if self.major == 0:
if self.minor is not None and self.minor != 0:
return self.next_minor()
if self.precision == 1:
return self.next_major()
elif self.precision == 2:
return self.next_minor()
return self.next_patch()
return self.stable.next_major()
@property
def min(self) -> "Version":
return self
@property
def max(self) -> "Version":
return self
@property
def full_max(self) -> "Version":
return self
@property
def include_min(self) -> bool:
return True
@property
def include_max(self) -> bool:
return True
def is_any(self) -> bool:
return False
def is_empty(self) -> bool:
return False
def is_simple(self) -> bool:
return True
def allows(self, version: "Version") -> bool:
if version is None:
return False
_this, _other = self, version
# allow weak equality to allow `3.0.0+local.1` for `3.0.0`
if not _this.is_local() and _other.is_local():
_other = _other.without_local()
elif _this.is_local() and not _other.is_local():
_this = _this.without_local()
# allow weak equality to allow `3.0.0-1` for `3.0.0`
if not _this.is_postrelease() and _other.is_postrelease():
_other = _other.without_postrelease()
elif _this.without_postrelease() and not _other.without_postrelease():
_this = _this.without_postrelease()
return _this == _other
def allows_all(self, other: "VersionConstraint") -> bool:
return other.is_empty() or (
self.allows(other) if isinstance(other, self.__class__) else other == self
)
def allows_any(self, other: "VersionConstraint") -> bool:
return other.allows(self)
def intersect(
self, other: "VersionConstraint"
) -> Union["Version", EmptyConstraint]:
if other.allows(self):
return self
return EmptyConstraint()
def union(self, other: "VersionConstraint") -> "VersionConstraint":
from poetry.core.semver.version_range import VersionRange
if other.allows(self):
return other
if isinstance(other, VersionRangeConstraint):
if self.allows(other.min):
return VersionRange(
other.min,
other.max,
include_min=True,
include_max=other.include_max,
)
if self.allows(other.max):
return VersionRange(
other.min,
other.max,
include_min=other.include_min,
include_max=True,
)
return VersionUnion.of(self, other)
def difference(
self, other: "VersionConstraint"
) -> Union["Version", EmptyConstraint]:
if other.allows(self):
return EmptyConstraint()
return self
def __str__(self) -> str:
return self.text
def __repr__(self) -> str:
return f"<Version {str(self)}>"
def __eq__(self, other: object) -> bool:
from poetry.core.semver.version_range import VersionRange
if isinstance(other, VersionRange):
return (
self == other.min
and self == other.max
and (other.include_min or other.include_max)
)
return super().__eq__(other)
@classmethod
def from_parts(
cls,
major: int,
minor: Optional[int] = None,
patch: Optional[int] = None,
extra: Optional[Union[int, Tuple[int, ...]]] = None,
pre: Optional[ReleaseTag] = None,
post: Optional[ReleaseTag] = None,
dev: Optional[ReleaseTag] = None,
local: "LocalSegmentType" = None,
*,
epoch: int = 0,
) -> "Version":
return cls(
release=Release(major=major, minor=minor, patch=patch, extra=extra),
pre=pre,
post=post,
dev=dev,
local=local,
epoch=epoch,
)