2525
2626# stdlib
2727import re
28- from typing import Dict , Generator , Iterable , Sequence , Tuple , Union
28+ from typing import Dict , Generator , Iterable , Sequence , Tuple , Type , TypeVar , Union
2929
3030# 3rd party
3131from typing_extensions import final
3232
3333__all__ = ["Version" ]
3434
35+ _V = TypeVar ("_V" , bound = "Version" )
36+
3537
3638@final
3739class Version (Tuple [int , int , int ]):
@@ -84,8 +86,8 @@ def minor(self): # noqa: D102
8486 def patch (self ): # noqa: D102
8587 return self [2 ]
8688
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
8991
9092 return t
9193
@@ -129,7 +131,7 @@ def __eq__(self, other) -> bool:
129131 """
130132 Returns whether this version is equal to the other version.
131133
132- :type other: str, float, Version
134+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
133135 """
134136
135137 other = _prep_for_eq (other )
@@ -144,7 +146,7 @@ def __gt__(self, other) -> bool:
144146 """
145147 Returns whether this version is greater than the other version.
146148
147- :type other: str, float, Version
149+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
148150 """
149151
150152 other = _prep_for_eq (other )
@@ -158,7 +160,7 @@ def __lt__(self, other) -> bool:
158160 """
159161 Returns whether this version is less than the other version.
160162
161- :type other: str, float, Version
163+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
162164 """
163165
164166 other = _prep_for_eq (other )
@@ -172,7 +174,7 @@ def __ge__(self, other) -> bool:
172174 """
173175 Returns whether this version is greater than or equal to the other version.
174176
175- :type other: str, float, Version
177+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
176178 """
177179
178180 other = _prep_for_eq (other )
@@ -186,7 +188,7 @@ def __le__(self, other) -> bool:
186188 """
187189 Returns whether this version is less than or equal to the other version.
188190
189- :type other: str, float, Version
191+ :type other: :class:` str`, :class:` float`, :class:`~. Version`
190192 """
191193
192194 other = _prep_for_eq (other )
@@ -197,7 +199,7 @@ def __le__(self, other) -> bool:
197199 return tuple (self )[:len (other )] <= other
198200
199201 @classmethod
200- def from_str (cls , version_string : str ) -> "Version" :
202+ def from_str (cls : Type [ _V ] , version_string : str ) -> _V :
201203 """
202204 Create a :class:`~.Version` from a :class:`str`.
203205
@@ -209,7 +211,7 @@ def from_str(cls, version_string: str) -> "Version":
209211 return cls (* _iter_string (version_string ))
210212
211213 @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 :
213215 """
214216 Create a :class:`~.Version` from a :class:`tuple`.
215217
@@ -226,7 +228,7 @@ def from_tuple(cls, version_tuple: Tuple[Union[str, int], ...]) -> "Version":
226228 return cls (* (int (x ) for x in version_tuple [:3 ]))
227229
228230 @classmethod
229- def from_float (cls , version_float : float ) -> "Version" :
231+ def from_float (cls : Type [ _V ] , version_float : float ) -> _V :
230232 """
231233 Create a :class:`~.Version` from a :class:`float`.
232234
@@ -250,7 +252,7 @@ def _asdict(self) -> Dict[str, int]:
250252 "patch" : self .patch ,
251253 }
252254
253- def _replace (self , ** kwargs ) -> "Version" :
255+ def _replace (self : _V , ** kwargs ) -> _V :
254256 """
255257 Return a new instance of the named tuple replacing specified fields with new values.
256258
@@ -262,7 +264,7 @@ def _replace(self, **kwargs) -> "Version":
262264 return self .__class__ (** {** self ._asdict (), ** kwargs })
263265
264266 @classmethod
265- def _make (cls , iterable : Iterable [Union [str , int ]]) -> "Version" :
267+ def _make (cls : Type [ _V ] , iterable : Iterable [Union [str , int ]]) -> _V :
266268 """
267269 Class method that makes a new instance from an existing sequence or iterable.
268270
0 commit comments