Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions nipype/external/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import warnings

import pytest

from nipype.external.version import LooseVersion as Vendored

with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
from distutils.version import LooseVersion as Original
except ImportError:
pytest.skip()


@pytest.mark.parametrize("v1, v2", [("0.0.0", "0.0.0"), ("0.0.0", "1.0.0")])
def test_LooseVersion_compat(v1, v2):
vend1, vend2 = Vendored(v1), Vendored(v2)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
orig1, orig2 = Original(v1), Original(v2)

assert vend1 == orig1
assert orig1 == vend1
assert vend2 == orig2
assert orig2 == vend2
assert (vend1 == orig2) == (v1 == v2)
assert (vend1 < orig2) == (v1 < v2)
assert (vend1 > orig2) == (v1 > v2)
assert (vend1 <= orig2) == (v1 <= v2)
assert (vend1 >= orig2) == (v1 >= v2)
assert (orig1 == vend2) == (v1 == v2)
assert (orig1 < vend2) == (v1 < v2)
assert (orig1 > vend2) == (v1 > v2)
assert (orig1 <= vend2) == (v1 <= v2)
assert (orig1 >= vend2) == (v1 >= v2)
24 changes: 20 additions & 4 deletions nipype/external/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# 2022.04.27 - Minor changes are made to the comments,
# - The StrictVersion class was removed
# - Black styling was applied
# 2022.05.11 - Refactor LooseVersion._cmp to permit comparisons with
# distutils.version.LooseVersion
#

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

import sys
import re


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

def _cmp(self, other):
if isinstance(other, str):
other = LooseVersion(other)
elif not isinstance(other, LooseVersion):
return NotImplemented
other = self._coerce(other)

if self.version == other.version:
return 0
if self.version < other.version:
return -1
if self.version > other.version:
return 1

@staticmethod
def _coerce(other):
if isinstance(other, LooseVersion):
return other
elif isinstance(other, str):
return LooseVersion(other)
elif "distutils" in sys.modules:
# Using this check to avoid importing distutils and suppressing the warning
try:
from distutils.version import LooseVersion as deprecated
except ImportError:
return NotImplemented
if isinstance(other, deprecated):
return LooseVersion(str(other))
return NotImplemented