Skip to content

Commit aa99263

Browse files
committed
FIX: Make vendored LooseVersion comparable to distutils.version.LooseVersion
1 parent bf67449 commit aa99263

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

nipype/external/version.py

Lines changed: 20 additions & 4 deletions
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)