diff --git a/src/poetry/core/version/pep440/version.py b/src/poetry/core/version/pep440/version.py index 197fdbca3..73487703a 100644 --- a/src/poetry/core/version/pep440/version.py +++ b/src/poetry/core/version/pep440/version.py @@ -4,6 +4,8 @@ from typing import Any from typing import Optional from typing import Tuple +from typing import Type +from typing import TypeVar from typing import Union from poetry.core.version.pep440.segments import RELEASE_PHASE_ALPHA @@ -14,6 +16,8 @@ from poetry.core.version.pep440.segments import ReleaseTag +T = TypeVar("T", bound="PEP440Version") + # we use the phase "z" to ensure we always sort this after other phases _INF_TAG = ReleaseTag("z", math.inf) # noqa # we use the phase "" to ensure we always sort this before other phases @@ -133,7 +137,7 @@ def to_string(self, short: bool = False) -> str: return version_string @classmethod - def parse(cls, value: str) -> "PEP440Version": + def parse(cls: Type[T], value: str) -> T: from poetry.core.version.pep440.parser import parse_pep440 return parse_pep440(value, cls) @@ -159,13 +163,13 @@ def is_unstable(self) -> bool: def is_stable(self) -> bool: return not self.is_unstable() - def next_major(self) -> "PEP440Version": + def next_major(self: T) -> T: release = self.release if self.is_stable() or Release(self.release.major, 0, 0) < self.release: release = self.release.next_major() return self.__class__(epoch=self.epoch, release=release) - def next_minor(self) -> "PEP440Version": + def next_minor(self: T) -> T: release = self.release if ( self.is_stable() @@ -174,20 +178,20 @@ def next_minor(self) -> "PEP440Version": release = self.release.next_minor() return self.__class__(epoch=self.epoch, release=release) - def next_patch(self) -> "PEP440Version": + def next_patch(self: T) -> T: return self.__class__( epoch=self.epoch, release=self.release.next_patch() if self.is_stable() else self.release, ) - def next_prerelease(self, next_phase: bool = False) -> "PEP440Version": + def next_prerelease(self: T, next_phase: bool = False) -> "PEP440Version": if self.is_prerelease(): pre = self.pre.next_phase() if next_phase else self.pre.next() else: pre = ReleaseTag(RELEASE_PHASE_ALPHA) return self.__class__(epoch=self.epoch, release=self.release, pre=pre) - def next_postrelease(self) -> "PEP440Version": + def next_postrelease(self: T) -> T: if self.is_prerelease(): post = self.post.next() else: @@ -200,7 +204,7 @@ def next_postrelease(self) -> "PEP440Version": post=post, ) - def next_devrelease(self) -> "PEP440Version": + def next_devrelease(self: T) -> T: if self.is_prerelease(): dev = self.dev.next() else: @@ -209,12 +213,12 @@ def next_devrelease(self) -> "PEP440Version": epoch=self.epoch, release=self.release, pre=self.pre, dev=dev ) - def first_prerelease(self) -> "PEP440Version": + def first_prerelease(self: T) -> T: return self.__class__( epoch=self.epoch, release=self.release, pre=ReleaseTag(RELEASE_PHASE_ALPHA) ) - def replace(self, **kwargs: Any) -> "PEP440Version": + def replace(self: T, **kwargs: Any) -> "PEP440Version": return self.__class__( **{ **{ @@ -226,8 +230,8 @@ def replace(self, **kwargs: Any) -> "PEP440Version": } ) - def without_local(self) -> "PEP440Version": + def without_local(self: T) -> T: return self.replace(local=None) - def without_postrelease(self) -> "PEP440Version": + def without_postrelease(self: T) -> T: return self.replace(post=None)