-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
82 additions
and
24 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
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,24 @@ | ||
import re | ||
from typing import Tuple, Union | ||
|
||
|
||
def parse_version(s: str) -> Union[Tuple[int, int], Tuple[int, int, int]]: | ||
""" | ||
Parse version string and return tuple of integer parts to allow for comparison. | ||
This does not implement the full version spec for version parsing, see | ||
https://packaging.python.org/en/latest/specifications/version-specifiers/. It is a | ||
simplified approach, so we do not have to depend on the external packaging module. | ||
We only support correct ordering for major, mior, and micro segments, ie. version | ||
strings of the form X.Y and X.Y.Z. Versions with pre- and post-release segments are | ||
correctly parsed, but these segments are ignored, as well as development release | ||
segments. | ||
""" | ||
match = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", s) | ||
if not match: | ||
msg = f"could not parse version string {s}" | ||
raise ValueError(msg) | ||
if match.group(3): | ||
return int(match.group(1)), int(match.group(2)), int(match.group(3)) | ||
return int(match.group(1)), int(match.group(2)) |
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,24 @@ | ||
from iminuit._parse_version import parse_version | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"s,ref", | ||
[ | ||
("1.2", (1, 2)), | ||
("1.2.3", (1, 2, 3)), | ||
("1.2a1", (1, 2)), | ||
("1.2.3a1", (1, 2, 3)), | ||
("1.2.post1", (1, 2)), | ||
("1.2.3.post1", (1, 2, 3)), | ||
("1.2a1.dev1", (1, 2)), | ||
("1.2.3a1.dev1", (1, 2, 3)), | ||
], | ||
) | ||
def test_parse_version(s, ref): | ||
assert parse_version(s) == ref | ||
|
||
|
||
def test_parse_version_bad(): | ||
with pytest.raises(ValueError): | ||
parse_version("a.b.c") |