Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0ca281d

Browse filesBrowse files
authored
Merge pull request #389 from tomschr/feature/version.names
Trivial: Introduce public Version.NAMES class variable
2 parents bf8757a + 86fcf48 commit 0ca281d
Copy full SHA for 0ca281d

File tree

2 files changed

+15
-8
lines changed
Filter options

2 files changed

+15
-8
lines changed

‎changelog.d/pr389.trivial.rst

Copy file name to clipboard
+6Lines 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

Copy file name to clipboardExpand all lines: src/semver/version.py
+9-8Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.