Skip to content

Commit 758e24e

Browse files
authored
[BUGFIX] Fix verison guessing in GitHub Actions (#545)
* Fetch tags in GitHub Actions workflow to get correct guessed version. * Simplify version code * Update python-package.yml
1 parent 18f4a05 commit 758e24e

File tree

3 files changed

+8
-93
lines changed

3 files changed

+8
-93
lines changed

.github/workflows/python-package.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ jobs:
1919
python-version: ["3.9", "3.10", "3.11", "3.12"]
2020

2121
steps:
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0 # Ensures the full history is available
25+
fetch-tags: true # Ensures tags are fetched
2326
- name: Set up Python ${{ matrix.python-version }}
2427
uses: actions/setup-python@v3
2528
with:

nam/train/_version.py

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,29 @@
66
Version utility
77
"""
88

9-
from typing import Optional as _Optional
10-
11-
from .._version import __version__
12-
13-
14-
class IncomparableVersionError(ValueError):
15-
"""
16-
Error raised when two versions can't be compared.
17-
"""
18-
19-
pass
9+
from .._version import __version__ as _package_version
2010

2111

2212
class Version:
23-
def __init__(self, major: int, minor: int, patch: int, dev: _Optional[str] = None):
13+
def __init__(self, major: int, minor: int, patch: int):
2414
self.major = major
2515
self.minor = minor
2616
self.patch = patch
27-
self.dev = dev
28-
self.dev_int = self._parse_dev_int(dev)
2917

3018
@classmethod
3119
def from_string(cls, s: str):
32-
def special_case(s: str) -> _Optional[dict]:
33-
"""
34-
Regretful hacks
35-
"""
36-
# It seems like the git repo isn't accessible to setuptools_scm's version
37-
# guesser, so it comes up with this during install:
38-
if s == "0.1.dev1":
39-
# This will be fine.
40-
return {
41-
"major": 0,
42-
"minor": 1,
43-
"patch": 0,
44-
"dev": "dev1"
45-
}
46-
return None
47-
48-
if special_case(s) is not None:
49-
return cls(**special_case(s))
50-
51-
# Typical
5220
parts = s.split(".")
53-
if len(parts) == 3: # e.g. "0.7.1"
54-
dev = None
55-
elif len(parts) == 4: # e.g. "0.7.1.dev7"
56-
dev = parts[3]
57-
else:
58-
raise ValueError(f"Invalid version string {s}")
5921
try:
6022
major, minor, patch = [int(x) for x in parts[:3]]
6123
except ValueError as e:
6224
raise ValueError(f"Failed to parse version from string '{s}':\n{e}")
63-
return cls(major=major, minor=minor, patch=patch, dev=dev)
25+
return cls(major=major, minor=minor, patch=patch)
6426

6527
def __eq__(self, other) -> bool:
6628
return (
6729
self.major == other.major
6830
and self.minor == other.minor
6931
and self.patch == other.patch
70-
and self.dev == other.dev
7132
)
7233

7334
def __lt__(self, other) -> bool:
@@ -79,45 +40,16 @@ def __lt__(self, other) -> bool:
7940
return self.minor < other.minor
8041
if self.patch != other.patch:
8142
return self.patch < other.patch
82-
if self.dev != other.dev:
83-
# None is defined as least
84-
if self.dev is None and other.dev is not None:
85-
return True
86-
elif self.dev is not None and other.dev is None:
87-
return False
88-
assert self.dev is not None
89-
assert other.dev is not None
90-
if self.dev_int is None:
91-
raise IncomparableVersionError(
92-
f"Version {str(self)} has incomparable dev version {self.dev}"
93-
)
94-
if other.dev_int is None:
95-
raise IncomparableVersionError(
96-
f"Version {str(other)} has incomparable dev version {other.dev}"
97-
)
98-
return self.dev_int < other.dev_int
9943
raise RuntimeError(
10044
f"Unhandled comparison between versions {str(self)} and {str(other)}"
10145
)
10246

10347
def __str__(self) -> str:
10448
return f"{self.major}.{self.minor}.{self.patch}"
10549

106-
def _parse_dev_int(self, dev: _Optional[str]) -> _Optional[int]:
107-
"""
108-
Turn the string into an int that can be compared if possible.
109-
"""
110-
if dev is None:
111-
return None
112-
if not isinstance(dev, str):
113-
raise TypeError(f"Invalid dev string type {type(dev)}")
114-
if not dev.startswith("dev") or len(dev) <= 3: # "misc", "dev", etc
115-
return None
116-
return int(dev.removeprefix("dev"))
117-
11850

11951
PROTEUS_VERSION = Version(4, 0, 0)
12052

12153

12254
def get_current_version() -> Version:
123-
return Version.from_string(__version__)
55+
return Version.from_string(_package_version)

tests/test_nam/test_train/test_version.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,11 @@
1111
from nam.train import _version
1212

1313

14-
def test_dev_int():
15-
"""
16-
Assert that dev_int is properly parsed
17-
"""
18-
assert _version.Version(0, 0, 0).dev_int is None
19-
assert _version.Version(0, 0, 0, "dev").dev_int is None
20-
assert _version.Version(0, 0, 0, "misc").dev_int is None
21-
assert _version.Version(0, 0, 0, "dev11").dev_int == 11
22-
23-
2414
def test_eq():
2515
assert _version.Version(0, 0, 0) == _version.Version(0, 0, 0)
2616
assert _version.Version(0, 0, 0) != _version.Version(0, 0, 1)
2717
assert _version.Version(0, 0, 0) != _version.Version(0, 1, 0)
2818
assert _version.Version(0, 0, 0) != _version.Version(1, 0, 0)
29-
assert _version.Version(0, 0, 0) != _version.Version(0, 0, 0, dev="dev0")
30-
assert _version.Version(0, 0, 0) != _version.Version(0, 0, 0, dev="dev1")
3119

3220

3321
def test_lt():
@@ -40,14 +28,6 @@ def test_lt():
4028
assert not _version.Version(1, 2, 3) < _version.Version(0, 4, 5)
4129

4230

43-
def test_lt_incomparable():
44-
"""
45-
Assert that the error is properly raised for incomparable versions
46-
"""
47-
with _pytest.raises(_version.IncomparableVersionError):
48-
_version.Version(0, 0, 0, "incomparable") < _version.Version(0, 0, 0, "dev1")
49-
50-
5131
def test_current_version():
5232
"""
5333
Test that the current version is valid

0 commit comments

Comments
 (0)