Skip to content

Commit 1350b96

Browse files
committed
Merge pull request #3389 from embray/utils/minversion
Simplify, standardize, and reduce repetition of version checks
1 parent ff831c4 commit 1350b96

23 files changed

Lines changed: 197 additions & 92 deletions

‎astropy/__init__.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,12 @@ def _check_numpy():
5858
except ImportError:
5959
pass
6060
else:
61-
major, minor, rest = numpy.__version__.split(".", 2)
62-
rmajor, rminor, rest = __minimum_numpy_version__.split(".", 2)
63-
requirement_met = ((int(major), int(minor)) >=
64-
(int(rmajor), int(rminor)))
61+
from .utils import minversion
62+
requirement_met = minversion(numpy, __minimum_numpy_version__)
6563

6664
if not requirement_met:
67-
msg = ("numpy version {0} or later must be installed to use "
68-
"astropy".format(
69-
__minimum_numpy_version__))
65+
msg = ("Numpy version {0} or later must be installed to use "
66+
"Astropy".format(__minimum_numpy_version__))
7067
raise ImportError(msg)
7168

7269
return numpy

‎astropy/_erfa/core.py.templ‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ github repository for more about this.)
3434
from __future__ import absolute_import, division, print_function
3535

3636
import warnings
37-
from distutils.version import LooseVersion
3837

38+
from ..utils.compat import NUMPY_LT_1_8
3939
from ..utils.exceptions import AstropyUserWarning
4040

4141
import numpy
4242
from . import _core
4343

44-
NPYLT18 = LooseVersion(numpy.__version__) < LooseVersion('1.8')
45-
# TODO: remove the above variable and the code using it and make_outputs_scalar when numpy < 1.8 is no longer supported
44+
# TODO: remove the above variable and the code using it and make_outputs_scalar
45+
# when numpy < 1.8 is no longer supported
4646

4747
__all__ = ['ErfaError', 'ErfaWarning',
4848
{{ funcs|map(attribute='pyname')|surround("'","'")|join(", ") }},
@@ -192,7 +192,7 @@ def {{ func.pyname }}({{ func.args_by_inout('in|inout')|map(attribute='name')|jo
192192

193193
{%- if func.args_by_inout('in|inout') %}
194194
make_outputs_scalar = False
195-
if NPYLT18:
195+
if NUMPY_LT_1_8:
196196
# in numpy < 1.8, the iterator used below doesn't work with 0d/scalar arrays
197197
# so we replace all scalars with 1d arrays
198198
make_outputs_scalar = True
@@ -247,4 +247,4 @@ STATUS_CODES['{{ func.pyname }}'] = {{ stat.doc_info.statuscodes|string }}
247247
{% endif %}
248248
{%- endfor %}
249249
250-
{% endfor %}
250+
{% endfor %}

‎astropy/_erfa/core.pyx.templ‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ For more about the module and how to use it, see the ``core.py`` docstrings.
1717
from __future__ import absolute_import, division, print_function
1818

1919
import warnings
20-
from distutils.version import LooseVersion
2120

2221
from ..utils.exceptions import AstropyUserWarning
2322

‎astropy/coordinates/tests/test_earth.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
"""Test initalization of angles not already covered by the API tests"""
1010

1111
import functools
12-
from distutils import version
1312
import numpy as np
1413

1514
from ..earth import EarthLocation, ELLIPSOIDS
1615
from ..angles import Longitude, Latitude
1716
from ...tests.helper import pytest
17+
from ...utils import minversion
1818
from ... import units as u
1919

20-
NUMPY_VERSION = version.LooseVersion(np.__version__)
2120

2221
allclose_m14 = functools.partial(np.allclose, rtol=1.e-14, atol=1.e-14)
2322
allclose_m8 = functools.partial(np.allclose, rtol=1.e-8, atol=1.e-8)
2423

25-
if NUMPY_VERSION >= version.LooseVersion('1.7.0'):
24+
25+
if minversion(np, '1.7.0'):
2626
isclose_m14 = functools.partial(np.isclose, rtol=1.e-14, atol=1.e-14)
2727
isclose_m8 = functools.partial(np.isclose, rtol=1.e-8, atol=1.e-8)
2828
else:

‎astropy/coordinates/tests/test_matching.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import numpy as np
77
from numpy import testing as npt
8-
from distutils import version
98
from ...tests.helper import pytest
109

1110
from ... import units as u
11+
from ...utils import minversion
1212

1313

1414
"""
@@ -23,7 +23,7 @@
2323
except ImportError:
2424
HAS_SCIPY = False
2525

26-
if HAS_SCIPY and version.LooseVersion(scipy.__version__) > version.LooseVersion('0.12.0'):
26+
if HAS_SCIPY and minversion(scipy, '0.12.0', inclusive=False):
2727
OLDER_SCIPY = False
2828
else:
2929
OLDER_SCIPY = True

‎astropy/coordinates/tests/test_sky_coord.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import numpy as np
1616
from numpy import testing as npt
17-
from distutils import version
1817

1918
from ... import units as u
2019
from ...tests.helper import pytest, catch_warnings
@@ -24,6 +23,7 @@
2423
UnitSphericalRepresentation)
2524
from ...coordinates import Latitude, Longitude
2625
from ...time import Time
26+
from ...utils import minversion
2727
from ...utils.exceptions import AstropyDeprecationWarning
2828

2929
RA = 1.0 * u.deg
@@ -40,7 +40,7 @@
4040
except ImportError:
4141
HAS_SCIPY = False
4242

43-
if HAS_SCIPY and version.LooseVersion(scipy.__version__) > version.LooseVersion('0.12.0'):
43+
if HAS_SCIPY and minversion(scipy, '0.12.0', inclusive=False):
4444
OLDER_SCIPY = False
4545
else:
4646
OLDER_SCIPY = True

‎astropy/io/fits/util.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ def _array_from_file(infile, dtype, count, sep):
601601

602602
global CHUNKED_FROMFILE
603603
if CHUNKED_FROMFILE is None:
604-
if sys.platform == 'darwin' and LooseVersion(platform.mac_ver()[0]) < LooseVersion('10.9'):
604+
if (sys.platform == 'darwin' and
605+
LooseVersion(platform.mac_ver()[0]) < LooseVersion('10.9')):
605606
CHUNKED_FROMFILE = True
606607
else:
607608
CHUNKED_FROMFILE = False

‎astropy/table/column.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import weakref
77

88
from copy import deepcopy
9-
from distutils import version
109

1110
import numpy as np
1211
from numpy import ma
1312

1413
from ..units import Unit, Quantity
14+
from ..utils.compat import NUMPY_LT_1_8
1515
from ..utils.console import color_print
1616
from ..utils.metadata import MetaData
1717
from . import groups
@@ -20,7 +20,6 @@
2020

2121
from ..config import ConfigAlias
2222

23-
NUMPY_VERSION = version.LooseVersion(np.__version__)
2423

2524
AUTO_COLNAME = ConfigAlias(
2625
'0.4', 'AUTO_COLNAME', 'auto_colname',
@@ -992,8 +991,9 @@ def _fix_fill_value(self, val):
992991
3). Here we change the string to a byte string so that in Python 3 the
993992
isinstance(val, basestring) part fails.
994993
"""
995-
if (NUMPY_VERSION < version.LooseVersion('1.8.0') and
996-
isinstance(val, six.string_types) and (self.dtype.char not in 'SV')):
994+
995+
if (NUMPY_LT_1_8 and isinstance(val, six.string_types) and
996+
(self.dtype.char not in 'SV')):
997997
val = val.encode()
998998
return val
999999

‎astropy/table/pprint.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import numpy as np
1313

1414
from .. import log
15+
# Note, in numpy <= 1.6, some classes do not properly represent themselves.
16+
from ..utils.compat import NUMPY_LT_1_6_1
1517
from ..utils.console import Getch, color_print, terminal_size, conf
1618

1719
if six.PY3:
@@ -27,8 +29,7 @@ def default_format_func(format_, val):
2729

2830
### The first three functions are helpers for _auto_format_func
2931

30-
# In numpy <= 1.6, some classes do not properly represent themselves.
31-
NUMPY_LE_1P6 = [int(x) for x in np.__version__.split('.')[:2]] <= [1, 6]
32+
3233
def _use_val_tolist(format_func):
3334
"""Wrap format function to work with values converted to python equivalents.
3435
@@ -72,7 +73,7 @@ def _auto_format_func(format_, val):
7273
"""
7374
if six.callable(format_):
7475
format_func = lambda format_, val: format_(val)
75-
if NUMPY_LE_1P6:
76+
if NUMPY_LT_1_6_1:
7677
format_func = _use_val_tolist(format_func)
7778
try:
7879
out = format_func(format_, val)
@@ -103,7 +104,7 @@ def _auto_format_func(format_, val):
103104
return str(val)
104105

105106
for format_func in _possible_string_format_functions(format_):
106-
if NUMPY_LE_1P6:
107+
if NUMPY_LT_1_6_1:
107108
format_func = _use_val_tolist(format_func)
108109

109110
try:

‎astropy/table/row.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
unicode_literals)
55

66
import collections
7-
from distutils import version
87

98
import numpy as np
109
from numpy import ma
1110

1211
from ..extern import six
1312
from ..utils import deprecated
13+
from ..utils.compat import NUMPY_LT_1_8
1414

1515
class Row(object):
1616
"""A class to represent one row of a Table object.
@@ -143,12 +143,14 @@ def as_void(self):
143143
# ValueError: Setting void-array with object members using buffer. [numpy.ma.core]
144144
#
145145
# All we do here is re-raise with a more informative message
146-
if (six.text_type(err).startswith('Setting void-array with object members')
147-
and version.LooseVersion(np.__version__) < version.LooseVersion('1.8')):
148-
raise ValueError('Cannot convert masked table row with Object type columns '
149-
'using as_void(), due to a bug in numpy {0}. Please upgrade '
150-
'to numpy 1.8 or newer.'
151-
.format(np.__version__))
146+
msg = six.text_type(err)
147+
if ('Setting void-array with object members' in msg and
148+
NUMPY_LT_1_8):
149+
raise ValueError(
150+
'Cannot convert masked table row with Object type '
151+
'columns using as_void(), due to a bug in Numpy '
152+
'{0}. Please upgrade to Numpy 1.8 or newer.'.format(
153+
np.__version__))
152154
else:
153155
raise
154156
else:

0 commit comments

Comments
 (0)