Skip to content

Commit c5e732b

Browse files
committed
Fix #344 Allow empty string for bump method
The methods .bump_prerelease('') and .bump_build('') generates a prerelease and build part without the token.
1 parent 011f680 commit c5e732b

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

changelog.d/344.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Allow empty string or ``None``` as token in
2+
:meth:`Version.bump_build <semver.Version.bump_build>` and
3+
:meth:`Version.bump_prerelease <semver.Version.bump_prerelease>`.
4+

docs/usage/increase-parts-of-a-version_prereleases.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.. _increase-parts-of-a-version:
12
Increasing Parts of a Version Taking into Account Prereleases
23
=============================================================
34

docs/usage/raise-parts-of-a-version.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
Raising Parts of a Version
22
==========================
33

4+
.. note::
5+
6+
Keep in mind, "raising" the pre-release only will make your
7+
complete version *lower* than before.
8+
9+
For example, having version ``1.0.0`` and raising the pre-release
10+
will lead to ``1.0.0-rc.1``, but ``1.0.0-rc.1`` is smaller than ``1.0.0``.
11+
12+
If you search for a way to take into account this behavior, look for the
13+
method :meth:`Version.next_version <semver.version.Version.next_version>`
14+
in section :ref:`increase-parts-of-a-version`.
15+
16+
417
The ``semver`` module contains the following functions to raise parts of
518
a version:
619

@@ -14,6 +27,7 @@ a version:
1427
``build`` to ``None``.
1528
* :func:`Version.bump_build <semver.version.Version.bump_build>`: raises the build part.
1629

30+
1731
.. code-block:: python
1832
1933
>>> str(Version.parse("3.4.5-pre.2+build.4").bump_major())
@@ -29,7 +43,7 @@ a version:
2943
3044
Likewise the module level functions :func:`semver.bump_major`.
3145

32-
For the functions :func:`Version.bump_prerelease <semver.version.Version.bump_prerelease>` and :func:`Version.bump_build <semver.version.Version.bump_build>` it's possible to pass an empty string or ``None``. However,
46+
For the methods :func:`Version.bump_prerelease <semver.version.Version.bump_prerelease>` and :func:`Version.bump_build <semver.version.Version.bump_build>` it's possible to pass an empty string or ``None``. However,
3347
it gives different results::
3448

3549
.. code-block:: python

src/semver/version.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ def bump_prerelease(self, token: Optional[str] = "rc") -> "Version":
319319
'rc.2'
320320
>>> ver.bump_prerelease('').prerelease
321321
'1'
322+
>>> ver.bump_prerelease(None).prerelease
323+
'rc.1'
322324
"""
323325
cls = type(self)
324326
if self._prerelease is not None:
@@ -330,11 +332,9 @@ def bump_prerelease(self, token: Optional[str] = "rc") -> "Version":
330332
else:
331333
prerelease = str(token) + ".0"
332334

333-
# self._prerelease or (token or "rc") + ".0"
334335
prerelease = cls._increment_string(prerelease)
335336
return cls(self._major, self._minor, self._patch, prerelease)
336337

337-
# VersionPart = Union[int, Optional[str]]
338338
def bump_build(self, token: Optional[str] = "build") -> "Version":
339339
"""
340340
Raise the build part of the version, return a new object but leave self
@@ -359,6 +359,17 @@ def bump_build(self, token: Optional[str] = "build") -> "Version":
359359
else:
360360
build = str(token) + ".0"
361361

362+
# self._build or (token or "build") + ".0"
363+
build = cls._increment_string(build)
364+
if self._build is not None:
365+
build = self._build
366+
elif token == "":
367+
build = "0"
368+
elif token is None:
369+
build = "build.0"
370+
else:
371+
build = str(token) + ".0"
372+
362373
# self._build or (token or "build") + ".0"
363374
build = cls._increment_string(build)
364375
return cls(self._major, self._minor, self._patch, self._prerelease, build)

0 commit comments

Comments
 (0)