Exemple #1
0
    def bump_postrelease(self: _R, inc: int = 1) -> _R:
        """
        Get next postrelease version.

        Arguments:
            inc -- Increment for micro version.

        Examples:

            ```python
            Version("1.2.3").bump_postrelease()  # "1.2.3.post1"
            Version("1.2.3.post3").bump_postrelease()  # "1.2.3.post4"
            Version("1.2.3a5").bump_postrelease()  # "1.2.3.post1"
            Version("1.2.3.post4").bump_postrelease(2)  # "1.2.3.post6"
            ```

        Returns:
            A new copy.
        """
        post = (VersionParts.POST, max(inc, 1))
        base_post: Optional[Tuple[str, int]] = self._version.post
        if base_post:
            post = (VersionParts.POST, max(base_post[1], 1) + inc)
        base = BaseVersion(
            epoch=0,
            release=self._version.release,
            pre=None,
            post=post,
            dev=None,
            local=None,
        )
        return self._replace(base)
Exemple #2
0
    def bump_micro(self: _R, inc: int = 1) -> _R:
        """
        Get next micro version.

        Arguments:
            inc -- Increment for micro version.

        Examples:

            ```python
            Version("1.2.3").bump_micro()  # "1.2.4"
            Version("1.2.3.dev14").bump_micro()  # "1.2.4"
            Version("1.2.3a5").bump_micro()  # "1.2.4"
            Version("1.2.3rc3").bump_micro(2)  # "1.2.5"
            Version("1.2.3rc3").bump_micro(0)  # "1.2.3"
            ```

        Returns:
            A new copy.
        """
        if not self.is_stable:
            return self.get_stable().bump_micro(inc - 1)

        return self._replace(
            BaseVersion(
                epoch=0,
                release=(self.major, self.minor, self.micro + inc),
                pre=None,
                post=None,
                dev=None,
                local=None,
            ))
Exemple #3
0
    def bump_major(self: _R, inc: int = 1) -> _R:
        """
        Get next major version.

        Arguments:
            inc -- Increment for major version.

        Examples:

            ```python
            Version("1.2.3").bump_major()  # "2.0.0"
            Version("1.2.3.dev14").bump_major()  # "2.0.0"
            Version("1.2.3a5").bump_major()  # "2.0.0"
            Version("1.2.3rc3").bump_major(2)  # "3.0.0"
            Version("1.2.3rc3").bump_major(0)  # "1.0.0"
            ```

        Returns:
            A new copy.
        """
        if not self.is_stable and self.minor == 0 and self.micro == 0:
            return self.get_stable().bump_major(inc - 1)

        return self._replace(
            BaseVersion(
                epoch=0,
                release=(self.major + inc, 0, 0),
                pre=None,
                post=None,
                dev=None,
                local=None,
            ))
Exemple #4
0
    def get_stable(self: _R) -> _R:
        """
        Get stable version from pre- or post- release.

        Examples:

            ```python
            Version("1.2.3").get_stable() # "1.2.3"
            Version("2.1.0a2").get_stable() # "2.1.0"
            Version("1.2.5.post3").get_stable() # "1.2.5"
            ```

        Returns:
            A new instance.
        """
        return self._replace(
            BaseVersion(
                epoch=0,
                release=(self.major, self.minor, self.micro),
                pre=None,
                post=None,
                dev=None,
                local=None,
            )
        )
Exemple #5
0
 def _copy_base(self, **kwargs: Any) -> BaseVersion:
     base_kwargs = dict(
         epoch=self.base.epoch,
         release=self.base.release,
         pre=self.base.pre,
         post=self.base.post,
         dev=self.base.dev,
         local=self.base.local,
     )
     base_kwargs.update(kwargs)
     return BaseVersion(**base_kwargs)
Exemple #6
0
    def bump_prerelease(
        self: _R,
        inc: int = 1,
        release_type: Literal["rc", "alpha", "beta", "a", "b"] = None,
        bump_release: Literal["major", "minor", "micro"] = VersionParts.MICRO,
    ) -> _R:
        """
        Get next prerelease version.
        If version is stable - bump `micro` for a proper versioning as well.
        Defaults to `rc` pre-releases.

        Arguments:
            inc -- Increment for micro version.
            release_type -- Prerelease type: alpha, beta, rc.
            bump_release -- Release number to bump if version is stable.

        Examples:

            ```python
            Version("1.2.3").bump_prerelease()  # "1.2.4rc1"
            Version("1.2.3").bump_prerelease(bump_release="major")  # "2.0.0rc1"
            Version("1.2.3.dev14").bump_prerelease()  # "1.2.3rc1"
            Version("1.2.3a5").bump_prerelease()  # "1.2.3a6"
            Version("1.2.3rc3").bump_prerelease(2, "beta")  # "1.2.3rc5"
            ```

        Returns:
            A new copy.
        """
        prerelease_type = release_type or self.prerelease_type or VersionParts.RC
        increment = inc if not self.base.pre else (max(self.base.pre[-1], 1) +
                                                   inc)
        pre = (prerelease_type, increment)

        new_version = self._replace(self._copy_base(pre=pre))
        if new_version < self:
            prerelease_type = release_type or VersionParts.RC
            new_version = self.get_stable().bump_release(bump_release)

        if prerelease_type != self.prerelease_type:
            increment = inc

        base = BaseVersion(
            epoch=0,
            release=new_version.base.release,
            pre=(prerelease_type, increment),
            post=None,
            dev=None,
            local=None,
        )
        return self._replace(base)