Skip to content

FIX: Make vendored LooseVersion comparable to distutils.version.LooseVersion #3466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions nipype/external/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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)
orig1, orig2 = Original(v1), Original(v2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we catch this DeprecationWarning warning that occurs when initializing a distutils Version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you get one? I don't in 3.10.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, and i'm on 3.10.4...

DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  v = LooseVersion("0.0")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'm on 3.10.4 and I don't see that. Does it happen just on the first one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ pytest nipype/external/tests/test_version.py 
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.10.4, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/mathiasg/code/nipype/nipype, configfile: pytest.ini
collected 2 items                                                                                                                                            

nipype/external/tests/test_version.py ..                                                                                                               [100%]

====================================================================== warnings summary ======================================================================
nipype/interfaces/io.py:1406
  /Users/mathiasg/code/nipype/nipype/interfaces/io.py:1406: DeprecationWarning: invalid escape sequence '\w'
    field_name = re.match("\w+", field_name).group()

nipype/interfaces/utility/base.py:232
  /Users/mathiasg/code/nipype/nipype/interfaces/utility/base.py:232: DeprecationWarning: invalid escape sequence '\w'
    """Change the name of a file based on a mapped format string.

../../.pyenv/versions/3.10.4/envs/nipreps/lib/python3.10/site-packages/_pytest/config/__init__.py:1252
  /Users/mathiasg/.pyenv/versions/3.10.4/envs/nipreps/lib/python3.10/site-packages/_pytest/config/__init__.py:1252: PytestConfigWarning: Unknown config option: env
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

external/tests/test_version.py::test_LooseVersion_compat[0.0.0-0.0.0]
external/tests/test_version.py::test_LooseVersion_compat[0.0.0-0.0.0]
external/tests/test_version.py::test_LooseVersion_compat[0.0.0-1.0.0]
external/tests/test_version.py::test_LooseVersion_compat[0.0.0-1.0.0]
  /Users/mathiasg/code/nipype/nipype/external/tests/test_version.py:18: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
    orig1, orig2 = Original(v1), Original(v2)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

in any case, it's not a big deal

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'll wrap it. Thanks for confirming.


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