Skip to content

Commit 8263f60

Browse files
committed
Updated docs for versions.py
1 parent 7763665 commit 8263f60

File tree

2 files changed

+109
-13
lines changed

2 files changed

+109
-13
lines changed

doc-source/api/versions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
:members:
77
:autosummary:
88
:undoc-members:
9+
:special-members:

domdf_python_tools/versions.py

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ class Version(Tuple[int, int, int]):
3535
"""
3636

3737
major: int
38+
"The major version number."
39+
3840
minor: int
41+
"The minor version number."
42+
3943
patch: int
44+
"The patch number."
4045

4146
def __new__(cls, major=0, minor=0, patch=0) -> "Version":
4247
t: "Version" = super().__new__(cls, (int(major), int(minor), int(patch))) # type: ignore
@@ -85,57 +90,147 @@ def __getnewargs__(self):
8590
return tuple(self)
8691

8792
def __eq__(self, other) -> bool:
93+
"""
94+
Returns whether this version is equal to the other version.
95+
96+
:type other: str, float, Version
97+
"""
98+
8899
other = _prep_for_eq(other)
89-
shortest = min(len(self), (len(other)))
90100

91-
return self[:shortest] == other[:shortest]
101+
if other is NotImplemented:
102+
return NotImplemented
103+
else:
104+
shortest = min(len(self), (len(other)))
105+
return self[:shortest] == other[:shortest]
92106

93107
def __gt__(self, other) -> bool:
108+
"""
109+
Returns whether this version is greater than the other version.
110+
111+
:type other: str, float, Version
112+
"""
113+
94114
other = _prep_for_eq(other)
95-
return tuple(self) > other
115+
116+
if other is NotImplemented:
117+
return NotImplemented
118+
else:
119+
return tuple(self) > other
96120

97121
def __lt__(self, other) -> bool:
122+
"""
123+
Returns whether this version is less than the other version.
124+
125+
:type other: str, float, Version
126+
"""
127+
98128
other = _prep_for_eq(other)
99-
return tuple(self) < other
129+
130+
if other is NotImplemented:
131+
return NotImplemented
132+
else:
133+
return tuple(self) < other
100134

101135
def __ge__(self, other) -> bool:
136+
"""
137+
Returns whether this version is greater than or equal to the other version.
138+
139+
:type other: str, float, Version
140+
"""
141+
102142
other = _prep_for_eq(other)
103143

104-
return tuple(self)[:len(other)] >= other
144+
if other is NotImplemented:
145+
return NotImplemented
146+
else:
147+
return tuple(self)[:len(other)] >= other
105148

106149
def __le__(self, other) -> bool:
150+
"""
151+
Returns whether this version is less than or equal to the other version.
152+
153+
:type other: str, float, Version
154+
"""
155+
107156
other = _prep_for_eq(other)
108-
return tuple(self)[:len(other)] <= other
157+
158+
if other is NotImplemented:
159+
return NotImplemented
160+
else:
161+
return tuple(self)[:len(other)] <= other
109162

110163
@classmethod
111164
def from_str(cls, version_string: str) -> "Version":
165+
"""
166+
Create a :class:`~.Version` from a :class:`str`.
167+
168+
:param version_string: The version number.
169+
170+
:return: The created Version
171+
"""
172+
112173
return cls(*_iter_string(version_string))
113174

114175
@classmethod
115176
def from_tuple(cls, version_tuple: Tuple[Union[str, int], Union[str, int], Union[str, int]]) -> "Version":
177+
"""
178+
Create a :class:`~.Version` from a :class:`tuple`.
179+
180+
:param version_tuple: The version number.
181+
182+
:return: The created Version
183+
"""
184+
116185
return cls(*(int(x) for x in version_tuple))
117186

118187
@classmethod
119188
def from_float(cls, version_float: float) -> "Version":
189+
"""
190+
Create a :class:`~.Version` from a :class:`float`.
191+
192+
:param version_float: The version number.
193+
194+
:return: The created Version
195+
"""
196+
120197
return cls.from_str(str(version_float))
121198

122199

123200
def _iter_string(version_string: str) -> Generator[int, None, None]:
201+
"""
202+
Iterate over the version elements from a string.
203+
204+
:param version_string: The version as a string.
205+
206+
:return: Iterable elements of the version.
207+
"""
208+
124209
return (int(x) for x in version_string.split("."))
125210

126211

127212
def _iter_float(version_float: float) -> Generator[int, None, None]:
213+
"""
214+
Iterate over the version elements from a float.
215+
216+
:param version_float: The version as a float.
217+
218+
:return: Iterable elements of the version.
219+
"""
220+
128221
return _iter_string(str(version_float))
129222

130223

131-
def _prep_for_eq(other) -> Tuple[int, ...]:
224+
def _prep_for_eq(other: Union[str, float, Version], ) -> Union[Tuple[int, ...], NotImplemented]:
225+
"""
226+
Prepare 'other' for use in ``__eq__``, ``__le__``, ``__ge__``, ``__gt__``, and ``__lt__``.
227+
"""
228+
132229
if isinstance(other, str):
133-
other = tuple(_iter_string(other))
230+
return tuple(_iter_string(other))
134231
elif isinstance(other, (Version, Sequence)):
135-
other = tuple((int(x) for x in other))
232+
return tuple((int(x) for x in other))
136233
elif isinstance(other, (int, float)):
137-
other = tuple(_iter_float(other))
234+
return tuple(_iter_float(other))
138235
else: # pragma: no cover
139-
raise NotImplementedError
140-
141-
return other
236+
return NotImplemented

0 commit comments

Comments
 (0)