-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3763 from neutrinoceros/version_info
ENH: add a yt.version_info named tuple to ease version comparison downstream
- Loading branch information
Showing
7 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -15,8 +15,6 @@ | |
|
||
install_ccompiler() | ||
|
||
VERSION = "4.1.dev0" | ||
|
||
if os.path.exists("MANIFEST"): | ||
os.remove("MANIFEST") | ||
|
||
|
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import NamedTuple | ||
|
||
from packaging.version import Version | ||
|
||
__all__ = [ | ||
"__version__", | ||
"version_info", | ||
] | ||
|
||
__version__ = "4.1.dev0" # keep in sync with setup.cfg | ||
|
||
|
||
class VersionTuple(NamedTuple): | ||
""" | ||
A minimal representation of the current version number | ||
that can be used downstream to check the runtime version | ||
simply by comparing with builtin tuples, as can be done with | ||
the runtime Python version using sys.version_info | ||
https://docs.python.org/3/library/sys.html#sys.version_info | ||
""" | ||
|
||
major: int | ||
minor: int | ||
micro: int | ||
releaselevel: str | ||
serial: int | ||
|
||
|
||
def _parse_to_version_info(version_str: str) -> VersionTuple: | ||
# adapted from matplotlib 3.5 | ||
""" | ||
Parse a version string to a namedtuple analogous to sys.version_info. | ||
See: | ||
https://packaging.pypa.io/en/latest/version.html#packaging.version.parse | ||
https://docs.python.org/3/library/sys.html#sys.version_info | ||
""" | ||
v = Version(version_str) | ||
if v.pre is None and v.post is None and v.dev is None: | ||
return VersionTuple(v.major, v.minor, v.micro, "final", 0) | ||
elif v.dev is not None: | ||
return VersionTuple(v.major, v.minor, v.micro, "alpha", v.dev) | ||
elif v.pre is not None: | ||
releaselevel = {"a": "alpha", "b": "beta", "rc": "candidate"}.get( | ||
v.pre[0], "alpha" | ||
) | ||
return VersionTuple(v.major, v.minor, v.micro, releaselevel, v.pre[1]) | ||
elif v.post is not None: | ||
# fallback for v.post: guess-next-dev scheme from setuptools_scm | ||
return VersionTuple(v.major, v.minor, v.micro + 1, "alpha", v.post) | ||
else: | ||
return VersionTuple(v.major, v.minor, v.micro + 1, "alpha", 0) | ||
|
||
|
||
version_info = _parse_to_version_info(__version__) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
|
||
import yt | ||
from yt._version import VersionTuple, _parse_to_version_info | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version_str, expected", | ||
( | ||
("4.1.0", VersionTuple(4, 1, 0, "final", 0)), | ||
("6.2.5", VersionTuple(6, 2, 5, "final", 0)), | ||
("4.1.dev0", VersionTuple(4, 1, 0, "alpha", 0)), | ||
("4.1.0rc", VersionTuple(4, 1, 0, "candidate", 0)), | ||
("4.1.0rc1", VersionTuple(4, 1, 0, "candidate", 1)), | ||
("4.1.0rc2", VersionTuple(4, 1, 0, "candidate", 2)), | ||
), | ||
) | ||
def test_parse_version_info(version_str, expected): | ||
actual = _parse_to_version_info(version_str) | ||
assert actual == expected | ||
|
||
|
||
def test_version_tuple_comp(): | ||
# exercise comparison with builtin tuples | ||
# comparison results do not matter for this test | ||
|
||
yt.version_info > (4,) # noqa: B015 | ||
yt.version_info > (4, 1) # noqa: B015 | ||
yt.version_info > (4, 1, 0) # noqa: B015 | ||
yt.version_info < (4, 1, 0) # noqa: B015 | ||
yt.version_info <= (4, 1, 0) # noqa: B015 | ||
yt.version_info >= (4, 1, 0) # noqa: B015 | ||
yt.version_info == (4, 1, 0) # noqa: B015 | ||
|
||
assert isinstance(yt.version_info, tuple) |