-
Notifications
You must be signed in to change notification settings - Fork 70
refactor!: Drop VersionInfo in favor of tuple #124
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,15 @@ | ||
import re | ||
from typing import NamedTuple | ||
|
||
__all__ = ["version", "version_info"] | ||
|
||
|
||
version = "3.0.0b7" | ||
|
||
_re_version = re.compile(r"(\d+)\.(\d+)\.(\d+)(\D*)(\d*)") | ||
|
||
|
||
class VersionInfo(NamedTuple): | ||
major: int | ||
minor: int | ||
micro: int | ||
releaselevel: str | ||
serial: int | ||
|
||
@classmethod | ||
def from_str(cls, v: str) -> "VersionInfo": | ||
groups = _re_version.match(v).groups() # type: ignore | ||
major, minor, micro = map(int, groups[:3]) | ||
level = (groups[3] or "")[:1] | ||
if level == "a": | ||
level = "alpha" | ||
elif level == "b": | ||
level = "beta" | ||
elif level in ("c", "r"): | ||
level = "candidate" | ||
else: | ||
level = "final" | ||
serial = groups[4] | ||
serial = int(serial) if serial else 0 | ||
return cls(major, minor, micro, level, serial) | ||
|
||
def __str__(self) -> str: | ||
v = f"{self.major}.{self.minor}.{self.micro}" | ||
level = self.releaselevel | ||
if level and level != "final": | ||
v = f"{v}{level[:1]}{self.serial}" | ||
return v | ||
|
||
|
||
version_info = VersionInfo.from_str(version) | ||
version_info = (3, 0, 0, "beta", 7) | ||
# version_info has the same format as django.VERSION | ||
# https://github.com/django/django/blob/4a5048b036fd9e965515e31fdd70b0af72655cba/django/utils/version.py#L22 | ||
# | ||
# examples | ||
# "3.0.0" -> (3, 0, 0, "final", 0) | ||
# "3.0.0rc1" -> (3, 0, 0, "rc", 1) | ||
# "3.0.0b7" -> (3, 0, 0, "beta", 7) | ||
# "3.0.0a2" -> (3, 0, 0, "alpha", 2) | ||
# | ||
# also see tests/test_version.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,50 @@ | ||
import re | ||
import packaging | ||
from packaging.version import Version | ||
|
||
import graphql_server | ||
from graphql_server.version import VersionInfo, version, version_info | ||
from graphql_server.version import version, version_info | ||
|
||
_re_version = re.compile(r"(\d+)\.(\d+)\.(\d+)(?:([abc])(\d+))?$") | ||
RELEASE_LEVEL = {"alpha": "a", "beta": "b", "rc": "rc", "final": None} | ||
|
||
|
||
def test_create_version_info_from_fields(): | ||
v = VersionInfo(1, 2, 3, "alpha", 4) | ||
assert v.major == 1 | ||
assert v.minor == 2 | ||
assert v.micro == 3 | ||
assert v.releaselevel == "alpha" | ||
assert v.serial == 4 | ||
parsed_version = Version(version) | ||
|
||
|
||
def test_create_version_info_from_str(): | ||
v = VersionInfo.from_str("1.2.3") | ||
assert v.major == 1 | ||
assert v.minor == 2 | ||
assert v.micro == 3 | ||
assert v.releaselevel == "final" | ||
assert v.serial == 0 | ||
v = VersionInfo.from_str("1.2.3a4") | ||
assert v.major == 1 | ||
assert v.minor == 2 | ||
assert v.micro == 3 | ||
assert v.releaselevel == "alpha" | ||
assert v.serial == 4 | ||
v = VersionInfo.from_str("1.2.3beta4") | ||
assert v.major == 1 | ||
assert v.minor == 2 | ||
assert v.micro == 3 | ||
assert v.releaselevel == "beta" | ||
assert v.serial == 4 | ||
v = VersionInfo.from_str("12.34.56rc789") | ||
assert v.major == 12 | ||
assert v.minor == 34 | ||
assert v.micro == 56 | ||
assert v.releaselevel == "candidate" | ||
assert v.serial == 789 | ||
def test_valid_version() -> None: | ||
packaging.version.parse(version) | ||
|
||
|
||
def test_serialize_as_str(): | ||
v = VersionInfo(1, 2, 3, "final", 0) | ||
assert str(v) == "1.2.3" | ||
v = VersionInfo(1, 2, 3, "alpha", 4) | ||
assert str(v) == "1.2.3a4" | ||
def test_valid_version_info() -> None: | ||
"""version_info has to be a tuple[int, int, int, str, int]""" | ||
assert isinstance(version_info, tuple) | ||
assert len(version_info) == 5 | ||
|
||
major, minor, micro, release_level, serial = version_info | ||
assert isinstance(major, int) | ||
assert isinstance(minor, int) | ||
assert isinstance(micro, int) | ||
assert isinstance(release_level, str) | ||
assert isinstance(serial, int) | ||
|
||
def test_base_package_has_correct_version(): | ||
assert graphql_server.__version__ == version | ||
assert graphql_server.version == version | ||
|
||
def test_valid_version_release_level() -> None: | ||
if parsed_version.pre is not None: | ||
valid_release_levels = {v for v in RELEASE_LEVEL.values() if v is not None} | ||
assert parsed_version.pre[0] in valid_release_levels | ||
|
||
def test_base_package_has_correct_version_info(): | ||
assert graphql_server.__version_info__ is version_info | ||
assert graphql_server.version_info is version_info | ||
|
||
def test_valid_version_info_release_level() -> None: | ||
assert version_info[3] in RELEASE_LEVEL.keys() | ||
|
||
def test_version_has_correct_format(): | ||
assert isinstance(version, str) | ||
assert _re_version.match(version) | ||
|
||
def test_version_same_as_version_info() -> None: | ||
assert ( | ||
parsed_version.major, | ||
parsed_version.minor, | ||
parsed_version.micro, | ||
) == version_info[:3] | ||
|
||
def test_version_info_has_correct_fields(): | ||
assert isinstance(version_info, tuple) | ||
assert str(version_info) == version | ||
groups = _re_version.match(version).groups() # type: ignore | ||
assert version_info.major == int(groups[0]) | ||
assert version_info.minor == int(groups[1]) | ||
assert version_info.micro == int(groups[2]) | ||
if groups[3] is None: # pragma: no cover | ||
assert groups[4] is None | ||
else: # pragma: no cover | ||
assert version_info.releaselevel[:1] == groups[3] | ||
assert version_info.serial == int(groups[4]) | ||
release_level, serial = version_info[-2:] | ||
if parsed_version.is_prerelease: | ||
assert (RELEASE_LEVEL[release_level], serial) == parsed_version.pre | ||
else: | ||
assert (release_level, serial) == ("final", 0) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly the way we handle version strings in this library seems very 2.x-ish. I'm all for just removing the logic and replacing it with something along the lines of this:
Or drop the entire VersionInfo altogether, since this is a feature well-supported in
packaging
now. Since we are doing a major release, this shouldn't be a problem. What do you think? 😊There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I thought too. Not a fan of maintaining our own semver parser and as you said all the logics are already implemented in
packaging
.We could keep exposing
graphql_server.version = v3.0.0
andgraphql_server.version_info = (3, 0, 0, "final", 0)
and keep tests usingpackaging
to check ifversion
andversion_info
are spec-compliant and pointing to the same version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, let's just do the tuple one and completely drop the version info.