Skip to content

Commit 86fcf48

Browse files
committed
Introduce public Version.NAMES class variable
This class variable contains a tuple of strings that contains the names of all attributes of a Version (like "major", "minor" etc). In cases we need to have dynamical values, this makes it easier to iterate.
1 parent bf8757a commit 86fcf48

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

changelog.d/pr389.trivial.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Add public class variable :data:`Version.NAMES <semver.version.Version.NAMES>`.
2+
3+
This class variable contains a tuple of strings that contains the names of
4+
all attributes of a Version (like ``"major"``, ``"minor"`` etc).
5+
6+
In cases we need to have dynamical values, this makes it easier to iterate.

src/semver/version.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class Version:
6666
"""
6767

6868
__slots__ = ("_major", "_minor", "_patch", "_prerelease", "_build")
69+
70+
#: The names of the different parts of a version
71+
NAMES = tuple([item[1:] for item in __slots__])
72+
6973
#: Regex for number in a prerelease
7074
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
7175
#: Regex template for a semver version
@@ -398,13 +402,9 @@ def next_version(self, part: str, prerelease_token: str = "rc") -> "Version":
398402
:param prerelease_token: prefix string of prerelease, defaults to 'rc'
399403
:return: new object with the appropriate part raised
400404
"""
401-
validparts = {
402-
"major",
403-
"minor",
404-
"patch",
405-
"prerelease",
406-
# "build", # currently not used
407-
}
405+
cls = type(self)
406+
# "build" is currently not used, that's why we use [:-1]
407+
validparts = cls.NAMES[:-1]
408408
if part not in validparts:
409409
raise ValueError(
410410
"Invalid part. Expected one of {validparts}, but got {part!r}".format(
@@ -419,7 +419,8 @@ def next_version(self, part: str, prerelease_token: str = "rc") -> "Version":
419419
):
420420
return version.replace(prerelease=None, build=None)
421421

422-
if part in ("major", "minor", "patch"):
422+
# Only check the main parts:
423+
if part in cls.NAMES[:3]:
423424
return getattr(version, "bump_" + part)()
424425

425426
if not version.prerelease:

0 commit comments

Comments
 (0)