Skip to content

Commit 9c8cd1f

Browse files
committed
RF: Use importlib_resources over pkg_resources
1 parent 4ac1c0a commit 9c8cd1f

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

nibabel/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,16 @@ def bench(label=None, verbose=1, extra_argv=None):
171171
code : ExitCode
172172
Returns the result of running the tests as a ``pytest.ExitCode`` enum
173173
"""
174-
from pkg_resources import resource_filename
174+
try:
175+
from importlib.resources import as_file, files
176+
except ImportError:
177+
from importlib_resources import as_file, files
175178

176-
config = resource_filename('nibabel', 'benchmarks/pytest.benchmark.ini')
177179
args = []
178180
if extra_argv is not None:
179181
args.extend(extra_argv)
180-
args.extend(['-c', config])
181-
return test(label, verbose, extra_argv=args)
182+
183+
config_path = files('nibabel') / 'benchmarks/pytest.benchmark.ini'
184+
with as_file(config_path) as config:
185+
args.extend(['-c', str(config)])
186+
return test(label, verbose, extra_argv=args)

nibabel/testing/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Utilities for testing"""
10+
from __future__ import annotations
1011

1112
import os
1213
import re
1314
import sys
15+
import typing as ty
1416
import unittest
1517
import warnings
1618
from contextlib import nullcontext
@@ -19,24 +21,34 @@
1921
import numpy as np
2022
import pytest
2123
from numpy.testing import assert_array_equal
22-
from pkg_resources import resource_filename
2324

2425
from .helpers import assert_data_similar, bytesio_filemap, bytesio_round_trip
2526
from .np_features import memmap_after_ufunc
2627

28+
try:
29+
from importlib.abc import Traversable
30+
from importlib.resources import as_file, files
31+
except ImportError: # PY38
32+
from importlib_resources import as_file, files
33+
from importlib_resources.abc import Traversable
2734

28-
def test_data(subdir=None, fname=None):
35+
36+
def test_data(
37+
subdir: ty.Literal['gifti', 'nicom', 'externals'] | None = None,
38+
fname: str | None = None,
39+
) -> Traversable:
40+
parts: tuple[str, ...]
2941
if subdir is None:
30-
resource = os.path.join('tests', 'data')
42+
parts = ('tests', 'data')
3143
elif subdir in ('gifti', 'nicom', 'externals'):
32-
resource = os.path.join(subdir, 'tests', 'data')
44+
parts = (subdir, 'tests', 'data')
3345
else:
3446
raise ValueError(f'Unknown test data directory: {subdir}')
3547

3648
if fname is not None:
37-
resource = os.path.join(resource, fname)
49+
parts += (fname,)
3850

39-
return resource_filename('nibabel', resource)
51+
return files('nibabel').joinpath(*parts)
4052

4153

4254
# set path to example data

nibabel/tests/test_init.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import pathlib
12
from unittest import mock
23

34
import pytest
4-
from pkg_resources import resource_filename
5+
6+
try:
7+
from importlib.resources import as_file, files
8+
except ImportError:
9+
from importlib_resources import as_file, files
510

611
import nibabel as nib
712

@@ -38,12 +43,11 @@ def test_nibabel_test_errors():
3843

3944

4045
def test_nibabel_bench():
41-
expected_args = ['-c', '--pyargs', 'nibabel']
46+
config_path = files('nibabel') / 'benchmarks/pytest.benchmark.ini'
47+
if not isinstance(config_path, pathlib.Path):
48+
raise unittest.SkipTest('Package is not unpacked; could get temp path')
4249

43-
try:
44-
expected_args.insert(1, resource_filename('nibabel', 'benchmarks/pytest.benchmark.ini'))
45-
except:
46-
raise unittest.SkipTest('Not installed')
50+
expected_args = ['-c', str(config_path), '--pyargs', 'nibabel']
4751

4852
with mock.patch('pytest.main') as pytest_main:
4953
nib.bench(verbose=0)

nibabel/tests/test_testing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ def test_assert_re_in(regex, entries):
171171

172172

173173
def test_test_data():
174-
assert test_data() == data_path
175-
assert test_data() == os.path.abspath(
174+
assert str(test_data()) == str(data_path)
175+
assert str(test_data()) == os.path.abspath(
176176
os.path.join(os.path.dirname(__file__), '..', 'tests', 'data')
177177
)
178178
for subdir in ('nicom', 'gifti', 'externals'):
179-
assert test_data(subdir) == os.path.join(data_path[:-10], subdir, 'tests', 'data')
179+
assert str(test_data(subdir)) == os.path.join(
180+
data_path.parent.parent, subdir, 'tests', 'data'
181+
)
180182
assert os.path.exists(test_data(subdir))
181183
assert not os.path.exists(test_data(subdir, 'doesnotexist'))
182184

0 commit comments

Comments
 (0)