Skip to content

Trivial: Introduce public Version.NAMES class variable #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.d/pr389.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Add public class variable :data:`Version.NAMES <semver.version.Version.NAMES>`.

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.
17 changes: 9 additions & 8 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class Version:
"""

__slots__ = ("_major", "_minor", "_patch", "_prerelease", "_build")

#: The names of the different parts of a version
NAMES = tuple([item[1:] for item in __slots__])

#: Regex for number in a prerelease
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
#: Regex template for a semver version
Expand Down Expand Up @@ -398,13 +402,9 @@ def next_version(self, part: str, prerelease_token: str = "rc") -> "Version":
:param prerelease_token: prefix string of prerelease, defaults to 'rc'
:return: new object with the appropriate part raised
"""
validparts = {
"major",
"minor",
"patch",
"prerelease",
# "build", # currently not used
}
cls = type(self)
# "build" is currently not used, that's why we use [:-1]
validparts = cls.NAMES[:-1]
if part not in validparts:
raise ValueError(
"Invalid part. Expected one of {validparts}, but got {part!r}".format(
Expand All @@ -419,7 +419,8 @@ def next_version(self, part: str, prerelease_token: str = "rc") -> "Version":
):
return version.replace(prerelease=None, build=None)

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

if not version.prerelease:
Expand Down