From 325080e3f9f0edd6c892c04c17293521ddc14101 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 4 Feb 2020 14:57:23 -0500 Subject: [PATCH 1/4] TEST: Style cleanup --- nibabel/tests/test_arrayproxy.py | 8 +++---- nibabel/tests/test_arraywriters.py | 7 +++--- nibabel/tests/test_batteryrunners.py | 36 +++++----------------------- nibabel/tests/test_casting.py | 8 ++----- 4 files changed, 14 insertions(+), 45 deletions(-) diff --git a/nibabel/tests/test_arrayproxy.py b/nibabel/tests/test_arrayproxy.py index b00af7d90f..7b2fcca384 100644 --- a/nibabel/tests/test_arrayproxy.py +++ b/nibabel/tests/test_arrayproxy.py @@ -73,7 +73,7 @@ def test_init(): assert ap.shape == shape # shape should be read only with pytest.raises(AttributeError): - setattr(ap, 'shape', shape) + ap.shape = shape # Get the data assert_array_equal(np.asarray(ap), arr) # Check we can modify the original header without changing the ap version @@ -323,10 +323,8 @@ def check_mmap(hdr, offset, proxy_class, assert not unscaled_is_mmap assert not back_is_mmap else: - assert (unscaled_is_mmap == - (viral_memmap or unscaled_really_mmap)) - assert (back_is_mmap == - (viral_memmap or scaled_really_mmap)) + assert unscaled_is_mmap == (viral_memmap or unscaled_really_mmap) + assert back_is_mmap == (viral_memmap or scaled_really_mmap) if scaled_really_mmap: assert back_data.mode == expected_mode del prox, back_data diff --git a/nibabel/tests/test_arraywriters.py b/nibabel/tests/test_arraywriters.py index fa24b37102..8f30f04321 100644 --- a/nibabel/tests/test_arraywriters.py +++ b/nibabel/tests/test_arraywriters.py @@ -14,11 +14,10 @@ from ..casting import int_abs, type_info, shared_range, on_powerpc from ..volumeutils import array_from_file, apply_read_scaling, _dt_min_max -from numpy.testing import (assert_array_almost_equal, - assert_array_equal) +from numpy.testing import assert_array_almost_equal, assert_array_equal import pytest from ..testing_pytest import (assert_allclose_safely, suppress_warnings, - error_warnings) + error_warnings) FLOAT_TYPES = np.sctypes['float'] @@ -532,7 +531,7 @@ def test_nan2zero(): # Deprecation warning for nan2zero as argument to `to_fileobj` with error_warnings(): with pytest.raises(DeprecationWarning): - aw.to_fileobj(BytesIO(), 'F', False) + aw.to_fileobj(BytesIO(), 'F', False) with pytest.raises(DeprecationWarning): aw.to_fileobj(BytesIO(), 'F', nan2zero=False) # Error if nan2zero is not the value set at initialization diff --git a/nibabel/tests/test_batteryrunners.py b/nibabel/tests/test_batteryrunners.py index 883054ff96..69f18b75ac 100644 --- a/nibabel/tests/test_batteryrunners.py +++ b/nibabel/tests/test_batteryrunners.py @@ -159,43 +159,19 @@ def test_logging(): def test_checks(): battrun = BatteryRunner((chk1,)) reports = battrun.check_only({}) - assert (reports[0] == - Report(KeyError, - 20, - 'no "testkey"', - '')) + assert reports[0] == Report(KeyError, 20, 'no "testkey"', '') obj, reports = battrun.check_fix({}) - assert (reports[0] == - Report(KeyError, - 20, - 'no "testkey"', - 'added "testkey"')) + assert reports[0] == Report(KeyError, 20, 'no "testkey"', 'added "testkey"') assert obj == {'testkey': 1} battrun = BatteryRunner((chk1, chk2)) reports = battrun.check_only({}) - assert (reports[0] == - Report(KeyError, - 20, - 'no "testkey"', - '')) - assert (reports[1] == - Report(KeyError, - 20, - 'no "testkey"', - '')) + assert reports[0] == Report(KeyError, 20, 'no "testkey"', '') + assert reports[1] == Report(KeyError, 20, 'no "testkey"', '') obj, reports = battrun.check_fix({}) # In the case of fix, the previous fix exposes a different error # Note, because obj is mutable, first and second point to modified # (and final) dictionary output_obj = {'testkey': 0} - assert (reports[0] == - Report(KeyError, - 20, - 'no "testkey"', - 'added "testkey"')) - assert (reports[1] == - Report(ValueError, - 10, - '"testkey" != 0', - 'set "testkey" to 0')) + assert reports[0] == Report(KeyError, 20, 'no "testkey"', 'added "testkey"') + assert reports[1] == Report(ValueError, 10, '"testkey" != 0', 'set "testkey" to 0') assert obj == output_obj diff --git a/nibabel/tests/test_casting.py b/nibabel/tests/test_casting.py index 791cdacedb..3fe74dfb8b 100644 --- a/nibabel/tests/test_casting.py +++ b/nibabel/tests/test_casting.py @@ -43,9 +43,7 @@ def test_shared_range(): if imax_roundtrip == imax: thresh_overflow = True if thresh_overflow: - assert np.all( - (bit_bigger == casted_mx) | - (bit_bigger == imax)) + assert np.all((bit_bigger == casted_mx) | (bit_bigger == imax)) else: assert np.all((bit_bigger <= casted_mx)) if it in np.sctypes['uint']: @@ -71,9 +69,7 @@ def test_shared_range(): if imin_roundtrip == imin: thresh_overflow = True if thresh_overflow: - assert np.all( - (bit_smaller == casted_mn) | - (bit_smaller == imin)) + assert np.all((bit_smaller == casted_mn) | (bit_smaller == imin)) else: assert np.all((bit_smaller >= casted_mn)) From 5f695b8c3fc06536aebcfdc5542ce7968df5cb62 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 4 Feb 2020 15:49:36 -0500 Subject: [PATCH 2/4] TEST: Simplify data/environment fixtures --- nibabel/tests/test_data.py | 17 ++++++----------- nibabel/tests/test_environment.py | 25 ++++++++++--------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/nibabel/tests/test_data.py b/nibabel/tests/test_data.py index e48356eb50..1c8812fe61 100644 --- a/nibabel/tests/test_data.py +++ b/nibabel/tests/test_data.py @@ -19,24 +19,19 @@ import pytest -from .test_environment import (with_environment, - DATA_KEY, - USER_KEY) +from .test_environment import with_environment, DATA_KEY, USER_KEY -@pytest.fixture() +@pytest.fixture def with_nimd_env(request, with_environment): DATA_FUNCS = {} DATA_FUNCS['home_dir_func'] = nibd.get_nipy_user_dir DATA_FUNCS['sys_dir_func'] = nibd.get_nipy_system_dir DATA_FUNCS['path_func'] = nibd.get_data_path - - def teardown_data_env(): - nibd.get_nipy_user_dir = DATA_FUNCS['home_dir_func'] - nibd.get_nipy_system_dir = DATA_FUNCS['sys_dir_func'] - nibd.get_data_path = DATA_FUNCS['path_func'] - - request.addfinalizer(teardown_data_env) + yield + nibd.get_nipy_user_dir = DATA_FUNCS['home_dir_func'] + nibd.get_nipy_system_dir = DATA_FUNCS['sys_dir_func'] + nibd.get_data_path = DATA_FUNCS['path_func'] def test_datasource(): diff --git a/nibabel/tests/test_environment.py b/nibabel/tests/test_environment.py index 6dc127c95f..e0514c337e 100644 --- a/nibabel/tests/test_environment.py +++ b/nibabel/tests/test_environment.py @@ -14,7 +14,7 @@ USER_KEY = 'NIPY_USER_DIR' -@pytest.fixture() +@pytest.fixture def with_environment(request): """Setup test environment for some functions that are tested in this module. In particular this functions stores attributes @@ -24,20 +24,15 @@ def with_environment(request): """ GIVEN_ENV = {} GIVEN_ENV['env'] = env.copy() - - - def teardown_environment(): - """Restore things that were remembered by the setup_environment function - """ - orig_env = GIVEN_ENV['env'] - # Pull keys out into list to avoid altering dictionary during iteration, - # causing python 3 error - for key in list(env.keys()): - if key not in orig_env: - del env[key] - env.update(orig_env) - - request.addfinalizer(teardown_environment) + yield + """Restore things that were remembered by the setup_environment function """ + orig_env = GIVEN_ENV['env'] + # Pull keys out into list to avoid altering dictionary during iteration, + # causing python 3 error + for key in list(env.keys()): + if key not in orig_env: + del env[key] + env.update(orig_env) def test_nipy_home(): From f3be7b238ba1e5e41eb475da7c406ac821aa2b8f Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 4 Feb 2020 16:00:29 -0500 Subject: [PATCH 3/4] TEST: Simplify, reformat test_data --- nibabel/tests/test_data.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/nibabel/tests/test_data.py b/nibabel/tests/test_data.py index 1c8812fe61..e5d5000438 100644 --- a/nibabel/tests/test_data.py +++ b/nibabel/tests/test_data.py @@ -157,8 +157,7 @@ def test_data_path(with_nimd_env): with open(tmpfile, 'wt') as fobj: fobj.write('[DATA]\n') fobj.write('path = %s\n' % '/path/two') - assert (get_data_path() == - tst_list + ['/path/two'] + old_pth) + assert get_data_path() == tst_list + ['/path/two'] + old_pth def test_find_data_dir(): @@ -201,10 +200,10 @@ def test_make_datasource(with_nimd_env): assert ds.version == '0.1' +@pytest.mark.xfail(raises=DataError) def test_bomber(): - with pytest.raises(DataError): - b = Bomber('bomber example', 'a message') - b.any_attribute # no error + b = Bomber('bomber example', 'a message') + b.any_attribute # no error def test_bomber_inspect(): @@ -213,13 +212,12 @@ def test_bomber_inspect(): def test_datasource_or_bomber(with_nimd_env): - pkg_def = dict( - relpath='pkg') + pkg_def = dict(relpath='pkg') with TemporaryDirectory() as tmpdir: nibd.get_data_path = lambda: [tmpdir] ds = datasource_or_bomber(pkg_def) with pytest.raises(DataError): - getattr(ds, 'get_filename') + ds.get_filename('some_file.txt') pkg_dir = pjoin(tmpdir, 'pkg') os.mkdir(pkg_dir) tmpfile = pjoin(pkg_dir, 'config.ini') @@ -235,4 +233,4 @@ def test_datasource_or_bomber(with_nimd_env): pkg_def['min version'] = '0.3' ds = datasource_or_bomber(pkg_def) # not OK with pytest.raises(DataError): - getattr(ds, 'get_filename') + ds.get_filename('some_file.txt') From 92c758aebd75b7944c351b00cf39ba733782bc86 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 4 Feb 2020 16:17:18 -0500 Subject: [PATCH 4/4] STY: Remove unused pkgutil import --- nibabel/optpkg.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nibabel/optpkg.py b/nibabel/optpkg.py index 3590cd3c00..81dae3010c 100644 --- a/nibabel/optpkg.py +++ b/nibabel/optpkg.py @@ -1,5 +1,4 @@ """ Routines to support optional packages """ -import pkgutil from distutils.version import LooseVersion from .tripwire import TripWire