Skip to content

RF: Avoid import-time loading of nose, mock and nibabel.testing #699

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 5 commits into from
Feb 7, 2019
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
25 changes: 19 additions & 6 deletions nibabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@

For more detailed information see the :ref:`manual`.
"""


def setup_test():
""" Set numpy print options to "legacy" for new versions of numpy

If imported into a file, nosetest will run this before any doctests.
"""
import numpy
from distutils.version import LooseVersion
if LooseVersion(numpy.__version__) >= LooseVersion('1.14'):
numpy.set_printoptions(legacy="1.13")


# module imports
from . import analyze as ana
from . import spm99analyze as spm99
Expand Down Expand Up @@ -67,19 +80,19 @@
from . import mriutils
from . import streamlines
from . import viewers
from .testing import setup_test

# Note test requirement for "mock". Requirement for "nose" tested by numpy.
try:
import mock
except ImportError:
import pkgutil

if not pkgutil.find_loader('mock'):
def test(*args, **kwargs):
raise RuntimeError('Need "mock" package for tests')
else:
from numpy.testing import Tester
test = Tester().test
bench = Tester().bench
del mock, Tester
del Tester

del pkgutil

from .pkg_info import get_pkg_info as _get_pkg_info

Expand Down
2 changes: 1 addition & 1 deletion nibabel/affines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from six.moves import reduce
from .testing import setup_test # noqa
from . import setup_test # noqa


class AffineError(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from platform import processor, machine

import numpy as np
from .testing import setup_test # noqa
from . import setup_test # noqa


class CastingError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/nicom/dwiparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'''
import numpy as np
import numpy.linalg as npl
from ..testing import setup_test as setup_module # noqa
from .. import setup_test as setup_module # noqa


def B2q(B, tol=None):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .spm99analyze import SpmAnalyzeHeader
from .casting import have_binary128
from .pydicom_compat import have_dicom, pydicom as pdcm
from .testing import setup_test # noqa
from . import setup_test # noqa

# nifti1 flat header definition for Analyze-like first 348 bytes
# first number in comments indicates offset in file header in bytes
Expand Down
14 changes: 6 additions & 8 deletions nibabel/optpkg.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
""" Routines to support optional packages """
import pkgutil
from distutils.version import LooseVersion

from six import string_types
from .tripwire import TripWire

try:
import nose
except ImportError:
have_nose = False
else:
if pkgutil.find_loader('nose'):
have_nose = True

from .tripwire import TripWire
else:
have_nose = False


def _check_pkg_version(pkg, min_version):
Expand Down Expand Up @@ -121,6 +118,7 @@ def optional_package(name, trip_msg=None, min_version=None):

def setup_module():
if have_nose:
import nose
raise nose.plugins.skip.SkipTest('No %s for these tests'
% name)
return pkg, False, setup_module
2 changes: 1 addition & 1 deletion nibabel/quaternions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import math
import numpy as np
from .testing import setup_test # noqa
from . import setup_test # noqa

MAX_FLOAT = np.maximum_sctype(np.float)
FLOAT_EPS = np.finfo(np.float).eps
Expand Down
10 changes: 0 additions & 10 deletions nibabel/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,3 @@ def assert_arr_dict_equal(dict1, dict2):
for key, value1 in dict1.items():
value2 = dict2[key]
assert_array_equal(value1, value2)


def setup_test():
""" Set numpy print options to "legacy" for new versions of numpy

If imported into a file, nosetest will run this before any doctests.
"""
from distutils.version import LooseVersion
if LooseVersion(np.__version__) >= LooseVersion('1.14'):
np.set_printoptions(legacy="1.13")
5 changes: 5 additions & 0 deletions nibabel/tests/test_optpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def test_basic():
assert_good('os.path')
# We never have package _not_a_package
assert_bad('_not_a_package')

# setup_module imports nose, so make sure we don't disrupt that
orig_import = builtins.__import__
def raise_Exception(*args, **kwargs):
if args[0] == 'nose':
return orig_import(*args, **kwargs)
raise Exception(
"non ImportError could be thrown by some malfunctioning module "
"upon import, and optional_package should catch it too")
Expand Down