Skip to content

Commit d0eb014

Browse files
committed
RF: Improve fallback version check, require PyPA packaging module
1 parent 6425c2a commit d0eb014

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

nibabel/pkg_info.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import sys
2-
import re
3-
from distutils.version import StrictVersion
2+
from packaging.version import Version
43
from . import _version
54

65
__version__ = _version.get_versions()['version']
76

87

9-
def _parse_version(version_str):
10-
""" Parse version string `version_str` in our format
11-
"""
12-
match = re.match(r'([0-9.]*\d)(.*)', version_str)
13-
if match is None:
14-
raise ValueError('Invalid version ' + version_str)
15-
return match.groups()
16-
17-
188
def _cmp(a, b):
199
""" Implementation of ``cmp`` for Python 3
2010
"""
@@ -27,6 +17,8 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
2717
To be valid, a version must have a numerical major version followed by a
2818
dot, followed by a numerical minor version. It may optionally be followed
2919
by a dot and a numerical micro version, and / or by an "extra" string.
20+
This comparator follows PEP 440 conventions for determining pre- and post-
21+
releases.
3022
*Any* extra string labels the version as pre-release, so `1.2.0somestring`
3123
compares as prior to (pre-release for) `1.2.0`, where `somestring` can be
3224
any string.
@@ -50,15 +42,10 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
5042
1
5143
>>> cmp_pkg_version('1.2.0dev', '1.2.0')
5244
-1
45+
>>> cmp_pkg_version('1.2.0.post1', '1.2.0')
46+
1
5347
"""
54-
version, extra = _parse_version(version_str)
55-
pkg_version, pkg_extra = _parse_version(pkg_version_str)
56-
if version != pkg_version:
57-
return _cmp(StrictVersion(version), StrictVersion(pkg_version))
58-
return (0 if extra == pkg_extra
59-
else 1 if extra == ''
60-
else -1 if pkg_extra == ''
61-
else _cmp(extra, pkg_extra))
48+
return _cmp(Version(version_str), Version(pkg_version_str))
6249

6350

6451
def pkg_commit_hash(pkg_path=None):

nibabel/tests/test_pkg_info.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" Testing package info
22
"""
33

4+
from packaging.version import Version
5+
46
import nibabel as nib
57
from nibabel.pkg_info import cmp_pkg_version
68
from ..info import VERSION
@@ -30,12 +32,14 @@ def test_fallback_version():
3032
This should only fail if we fail to bump nibabel.info.VERSION immediately
3133
after release
3234
"""
35+
ver = Version(nib.__version__)
36+
fallback = Version(VERSION)
3337
assert (
38+
# Releases have no local information, archive matches versioneer
39+
ver.local is None or
3440
# dev version should be larger than tag+commit-githash
35-
cmp_pkg_version(VERSION) >= 0 or
36-
# Allow VERSION bump to lag releases by one commit
37-
VERSION == nib.__version__ + 'dev'), \
38-
"nibabel.info.VERSION does not match current tag information"
41+
fallback >= ver), \
42+
"nibabel.info.VERSION does not match latest tag information"
3943

4044

4145
def test_cmp_pkg_version():
@@ -68,6 +72,10 @@ def test_cmp_pkg_version():
6872
assert_raises(ValueError, cmp_pkg_version, 'foo.2')
6973
assert_raises(ValueError, cmp_pkg_version, 'foo.2', '1.0')
7074
assert_raises(ValueError, cmp_pkg_version, '1.0', 'foo.2')
71-
assert_raises(ValueError, cmp_pkg_version, '1')
7275
assert_raises(ValueError, cmp_pkg_version, 'foo')
7376

77+
# Check dev/RC sequence
78+
seq = ('3.0.0dev', '3.0.0rc1', '3.0.0rc1.post.dev', '3.0.0rc2', '3.0.0rc2.post.dev', '3.0.0')
79+
for stage1, stage2 in zip(seq[:-1], seq[1:]):
80+
assert_equal(cmp_pkg_version(stage1, stage2), -1)
81+
assert_equal(cmp_pkg_version(stage2, stage1), 1)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ provides =
3131
python_requires = >=3.5.1
3232
install_requires =
3333
numpy >=1.12
34+
packaging
3435
tests_require =
3536
nose >=0.11
3637
pytest

0 commit comments

Comments
 (0)