Skip to content

Commit b410d08

Browse files
authored
Merge pull request #3466 from effigies/fix/looseversion_compat
FIX: Make vendored LooseVersion comparable to distutils.version.LooseVersion
2 parents 0df3e7c + f0fb21d commit b410d08

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

nipype/external/tests/test_version.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import warnings
2+
3+
import pytest
4+
5+
from nipype.external.version import LooseVersion as Vendored
6+
7+
with warnings.catch_warnings():
8+
warnings.simplefilter("ignore")
9+
try:
10+
from distutils.version import LooseVersion as Original
11+
except ImportError:
12+
pytest.skip()
13+
14+
15+
@pytest.mark.parametrize("v1, v2", [("0.0.0", "0.0.0"), ("0.0.0", "1.0.0")])
16+
def test_LooseVersion_compat(v1, v2):
17+
vend1, vend2 = Vendored(v1), Vendored(v2)
18+
with warnings.catch_warnings():
19+
warnings.simplefilter("ignore")
20+
orig1, orig2 = Original(v1), Original(v2)
21+
22+
assert vend1 == orig1
23+
assert orig1 == vend1
24+
assert vend2 == orig2
25+
assert orig2 == vend2
26+
assert (vend1 == orig2) == (v1 == v2)
27+
assert (vend1 < orig2) == (v1 < v2)
28+
assert (vend1 > orig2) == (v1 > v2)
29+
assert (vend1 <= orig2) == (v1 <= v2)
30+
assert (vend1 >= orig2) == (v1 >= v2)
31+
assert (orig1 == vend2) == (v1 == v2)
32+
assert (orig1 < vend2) == (v1 < v2)
33+
assert (orig1 > vend2) == (v1 > v2)
34+
assert (orig1 <= vend2) == (v1 <= v2)
35+
assert (orig1 >= vend2) == (v1 >= v2)

nipype/external/version.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# 2022.04.27 - Minor changes are made to the comments,
1313
# - The StrictVersion class was removed
1414
# - Black styling was applied
15+
# 2022.05.11 - Refactor LooseVersion._cmp to permit comparisons with
16+
# distutils.version.LooseVersion
1517
#
1618

1719
# distutils/version.py
@@ -38,6 +40,7 @@
3840
of the same class, thus must follow the same rules)
3941
"""
4042

43+
import sys
4144
import re
4245

4346

@@ -211,14 +214,27 @@ def __repr__(self):
211214
return "LooseVersion ('%s')" % str(self)
212215

213216
def _cmp(self, other):
214-
if isinstance(other, str):
215-
other = LooseVersion(other)
216-
elif not isinstance(other, LooseVersion):
217-
return NotImplemented
217+
other = self._coerce(other)
218218

219219
if self.version == other.version:
220220
return 0
221221
if self.version < other.version:
222222
return -1
223223
if self.version > other.version:
224224
return 1
225+
226+
@staticmethod
227+
def _coerce(other):
228+
if isinstance(other, LooseVersion):
229+
return other
230+
elif isinstance(other, str):
231+
return LooseVersion(other)
232+
elif "distutils" in sys.modules:
233+
# Using this check to avoid importing distutils and suppressing the warning
234+
try:
235+
from distutils.version import LooseVersion as deprecated
236+
except ImportError:
237+
return NotImplemented
238+
if isinstance(other, deprecated):
239+
return LooseVersion(str(other))
240+
return NotImplemented

0 commit comments

Comments
 (0)