Skip to content

Commit 0aeecbc

Browse files
committed
ENH: Schedule int64 warning to convert to error at 5.0
1 parent ca5cad2 commit 0aeecbc

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

nibabel/nifti1.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .arrayproxy import get_obj_dtype
2222
from .batteryrunners import Report
2323
from .casting import have_binary128
24+
from .deprecated import alert_future_error
2425
from .filebasedimages import SerializableImage
2526
from .optpkg import optional_package
2627
from .quaternions import fillpositive, mat2quat, quat2mat
@@ -1831,13 +1832,16 @@ def __init__(self, dataobj, affine, header=None, extra=None, file_map=None, dtyp
18311832
# already fail.
18321833
danger_dts = (np.dtype('int64'), np.dtype('uint64'))
18331834
if header is None and dtype is None and get_obj_dtype(dataobj) in danger_dts:
1834-
msg = (
1835+
alert_future_error(
18351836
f'Image data has type {dataobj.dtype}, which may cause '
1836-
'incompatibilities with other tools. This will error in '
1837-
'NiBabel 5.0. This warning can be silenced '
1838-
f'by passing the dtype argument to {self.__class__.__name__}().'
1837+
'incompatibilities with other tools.',
1838+
'5.0',
1839+
warning_rec='This warning can be silenced by passing the dtype argument'
1840+
f' to {self.__class__.__name__}().',
1841+
error_rec='To use this type, pass an explicit header or dtype argument'
1842+
f' to {self.__class__.__name__}().',
1843+
error_class=ValueError,
18391844
)
1840-
warnings.warn(msg, FutureWarning, stacklevel=2)
18411845
super().__init__(dataobj, affine, header, extra, file_map, dtype)
18421846
# Force set of s/q form when header is None unless affine is also None
18431847
if header is None and affine is not None:

nibabel/tests/test_nifti1.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
slice_order_codes,
3636
)
3737
from nibabel.optpkg import optional_package
38+
from nibabel.pkg_info import cmp_pkg_version
3839
from nibabel.spatialimages import HeaderDataError
3940
from nibabel.tmpdirs import InTemporaryDirectory
4041

@@ -766,16 +767,21 @@ class TestNifti1Pair(tana.TestAnalyzeImage, tspm.ImageScalingMixin):
766767
image_class = Nifti1Pair
767768
supported_np_types = TestNifti1PairHeader.supported_np_types
768769

769-
def test_int64_warning(self):
770+
def test_int64_warning_or_error(self):
770771
# Verify that initializing with (u)int64 data and no
771-
# header/dtype info produces a warning
772+
# header/dtype info produces a warning/error
772773
img_klass = self.image_class
773774
hdr_klass = img_klass.header_class
774775
for dtype in (np.int64, np.uint64):
775776
data = np.arange(24, dtype=dtype).reshape((2, 3, 4))
776-
with pytest.warns(FutureWarning):
777+
# Starts as a warning, transitions to error at 5.0
778+
if cmp_pkg_version('5.0') < 0:
779+
cm = pytest.raises(ValueError)
780+
else:
781+
cm = pytest.warns(FutureWarning)
782+
with cm:
777783
img_klass(data, np.eye(4))
778-
# No warnings if we're explicit, though
784+
# No problems if we're explicit, though
779785
with clear_and_catch_warnings():
780786
warnings.simplefilter('error')
781787
img_klass(data, np.eye(4), dtype=dtype)

0 commit comments

Comments
 (0)