Skip to content

Navigation Menu

Sign in
Appearance settings

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

Turn VersionInfo.parse into classmethod to allow subclasses #277

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 4 commits into from
Oct 16, 2020
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
8 changes: 4 additions & 4 deletions 8 semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ def match(self, match_expr):

return cmp_res in possibilities

@staticmethod
def parse(version):
@classmethod
def parse(cls, version):
"""
Parse version string to a VersionInfo instance.

Expand All @@ -651,7 +651,7 @@ def parse(version):
VersionInfo(major=3, minor=4, patch=5, \
prerelease='pre.2', build='build.4')
"""
match = VersionInfo._REGEX.match(version)
match = cls._REGEX.match(version)
if match is None:
raise ValueError("%s is not valid SemVer string" % version)

Expand All @@ -661,7 +661,7 @@ def parse(version):
version_parts["minor"] = int(version_parts["minor"])
version_parts["patch"] = int(version_parts["patch"])

return VersionInfo(**version_parts)
return cls(**version_parts)

def replace(self, **parts):
"""
Expand Down
18 changes: 18 additions & 0 deletions 18 test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,21 @@ def test_next_version_with_versioninfo(version, part, expected):
)
def test_repr(version, expected):
assert repr(version) == expected


def test_subclass_from_versioninfo():
class SemVerWithVPrefix(VersionInfo):
@classmethod
def parse(cls, version):
if not version.startswith("v"):
raise ValueError(
"{v}: not a valid semantic version tag".format(v=version)
)
return super(SemVerWithVPrefix, cls).parse(version[1:])

def __str__(self):
# Reconstruct the tag.
return "v" + super(SemVerWithVPrefix, self).__str__()

v = SemVerWithVPrefix.parse("v1.2.3")
assert str(v) == "v1.2.3"
Morty Proxy This is a proxified and sanitized view of the page, visit original site.