Skip to content

Commit 3e0062a

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 3e0062a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy