25
25
26
26
# stdlib
27
27
import re
28
- from typing import Dict , Generator , Iterable , Sequence , Tuple , Union
28
+ from typing import Dict , Generator , Iterable , Sequence , Tuple , Type , TypeVar , Union
29
29
30
30
# 3rd party
31
31
from typing_extensions import final
32
32
33
33
__all__ = ["Version" ]
34
34
35
+ _V = TypeVar ("_V" , bound = "Version" )
36
+
35
37
36
38
@final
37
39
class Version (Tuple [int , int , int ]):
@@ -84,8 +86,8 @@ def minor(self): # noqa: D102
84
86
def patch (self ): # noqa: D102
85
87
return self [2 ]
86
88
87
- def __new__ (cls , major = 0 , minor = 0 , patch = 0 ) -> "Version" : # noqa: D102
88
- t : "Version" = super ().__new__ (cls , (int (major ), int (minor ), int (patch ))) # type: ignore
89
+ def __new__ (cls : Type [ _V ] , major = 0 , minor = 0 , patch = 0 ) -> _V : # noqa: D102
90
+ t : _V = super ().__new__ (cls , (int (major ), int (minor ), int (patch ))) # type: ignore
89
91
90
92
return t
91
93
@@ -129,7 +131,7 @@ def __eq__(self, other) -> bool:
129
131
"""
130
132
Returns whether this version is equal to the other version.
131
133
132
- :type other: str, float, Version
134
+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
133
135
"""
134
136
135
137
other = _prep_for_eq (other )
@@ -144,7 +146,7 @@ def __gt__(self, other) -> bool:
144
146
"""
145
147
Returns whether this version is greater than the other version.
146
148
147
- :type other: str, float, Version
149
+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
148
150
"""
149
151
150
152
other = _prep_for_eq (other )
@@ -158,7 +160,7 @@ def __lt__(self, other) -> bool:
158
160
"""
159
161
Returns whether this version is less than the other version.
160
162
161
- :type other: str, float, Version
163
+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
162
164
"""
163
165
164
166
other = _prep_for_eq (other )
@@ -172,7 +174,7 @@ def __ge__(self, other) -> bool:
172
174
"""
173
175
Returns whether this version is greater than or equal to the other version.
174
176
175
- :type other: str, float, Version
177
+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
176
178
"""
177
179
178
180
other = _prep_for_eq (other )
@@ -186,7 +188,7 @@ def __le__(self, other) -> bool:
186
188
"""
187
189
Returns whether this version is less than or equal to the other version.
188
190
189
- :type other: str, float, Version
191
+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
190
192
"""
191
193
192
194
other = _prep_for_eq (other )
@@ -197,7 +199,7 @@ def __le__(self, other) -> bool:
197
199
return tuple (self )[:len (other )] <= other
198
200
199
201
@classmethod
200
- def from_str (cls , version_string : str ) -> "Version" :
202
+ def from_str (cls : Type [ _V ] , version_string : str ) -> _V :
201
203
"""
202
204
Create a :class:`~.Version` from a :class:`str`.
203
205
@@ -209,7 +211,7 @@ def from_str(cls, version_string: str) -> "Version":
209
211
return cls (* _iter_string (version_string ))
210
212
211
213
@classmethod
212
- def from_tuple (cls , version_tuple : Tuple [Union [str , int ], ...]) -> "Version" :
214
+ def from_tuple (cls : Type [ _V ] , version_tuple : Tuple [Union [str , int ], ...]) -> _V :
213
215
"""
214
216
Create a :class:`~.Version` from a :class:`tuple`.
215
217
@@ -226,7 +228,7 @@ def from_tuple(cls, version_tuple: Tuple[Union[str, int], ...]) -> "Version":
226
228
return cls (* (int (x ) for x in version_tuple [:3 ]))
227
229
228
230
@classmethod
229
- def from_float (cls , version_float : float ) -> "Version" :
231
+ def from_float (cls : Type [ _V ] , version_float : float ) -> _V :
230
232
"""
231
233
Create a :class:`~.Version` from a :class:`float`.
232
234
@@ -250,7 +252,7 @@ def _asdict(self) -> Dict[str, int]:
250
252
"patch" : self .patch ,
251
253
}
252
254
253
- def _replace (self , ** kwargs ) -> "Version" :
255
+ def _replace (self : _V , ** kwargs ) -> _V :
254
256
"""
255
257
Return a new instance of the named tuple replacing specified fields with new values.
256
258
@@ -262,7 +264,7 @@ def _replace(self, **kwargs) -> "Version":
262
264
return self .__class__ (** {** self ._asdict (), ** kwargs })
263
265
264
266
@classmethod
265
- def _make (cls , iterable : Iterable [Union [str , int ]]) -> "Version" :
267
+ def _make (cls : Type [ _V ] , iterable : Iterable [Union [str , int ]]) -> _V :
266
268
"""
267
269
Class method that makes a new instance from an existing sequence or iterable.
268
270
0 commit comments