Skip to content

Commit bba1b3c

Browse files
committed
ENH: Add function to schedule a warning to become an error
1 parent 9000943 commit bba1b3c

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

nibabel/deprecated.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Module to help with deprecating objects and classes
22
"""
3+
from __future__ import annotations
34

45
import warnings
6+
from typing import Type
57

68
from .deprecator import Deprecator
79
from .pkg_info import cmp_pkg_version
@@ -77,3 +79,40 @@ class VisibleDeprecationWarning(UserWarning):
7779

7880

7981
deprecate_with_version = Deprecator(cmp_pkg_version)
82+
83+
84+
def alert_future_error(
85+
msg: str,
86+
version: str,
87+
*,
88+
warning_class: Type[Warning] = FutureWarning,
89+
error_class: Type[Exception] = RuntimeError,
90+
warning_rec: str = '',
91+
error_rec: str = '',
92+
stacklevel: int = 2,
93+
):
94+
"""Warn or error with appropriate messages for changing functionality.
95+
96+
Parameters
97+
----------
98+
msg : str
99+
Description of the condition that led to the alert
100+
version : str
101+
NiBabel version at which the warning will become an error
102+
warning_class : subclass of Warning, optional
103+
Warning class to emit before version
104+
error_class : subclass of Exception, optional
105+
Error class to emit after version
106+
warning_rec : str, optional
107+
Guidance for suppressing the warning and avoiding the future error
108+
error_rec: str, optional
109+
Guidance for resolving the error
110+
stacklevel: int, optional
111+
Warnings stacklevel to provide; note that this will be incremented by
112+
1, so provide the stacklevel you would provide directly to warnings.warn()
113+
"""
114+
if cmp_pkg_version(version) >= 0:
115+
msg = f'{msg} This will error in NiBabel {version}. {warning_rec}'
116+
warnings.warn(msg.strip(), warning_class, stacklevel=stacklevel + 1)
117+
else:
118+
raise error_class(f'{msg} {error_rec}'.strip())

nibabel/tests/test_deprecated.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import pytest
77

88
from nibabel import pkg_info
9-
from nibabel.deprecated import FutureWarningMixin, ModuleProxy, deprecate_with_version
9+
from nibabel.deprecated import (
10+
FutureWarningMixin,
11+
ModuleProxy,
12+
alert_future_error,
13+
deprecate_with_version,
14+
)
1015
from nibabel.tests.test_deprecator import TestDeprecatorFunc as _TestDF
1116

1217

@@ -79,3 +84,28 @@ def func():
7984
assert func() == 99
8085
finally:
8186
pkg_info.cmp_pkg_version.__defaults__ = ('2.0',)
87+
88+
89+
def test_alert_future_error():
90+
with pytest.warns(FutureWarning):
91+
alert_future_error(
92+
'Message',
93+
'9999.9.9',
94+
warning_rec='Silence this warning by doing XYZ.',
95+
error_rec='Fix this issue by doing XYZ.',
96+
)
97+
with pytest.raises(RuntimeError):
98+
alert_future_error(
99+
'Message',
100+
'1.0.0',
101+
warning_rec='Silence this warning by doing XYZ.',
102+
error_rec='Fix this issue by doing XYZ.',
103+
)
104+
with pytest.raises(ValueError):
105+
alert_future_error(
106+
'Message',
107+
'1.0.0',
108+
warning_rec='Silence this warning by doing XYZ.',
109+
error_rec='Fix this issue by doing XYZ.',
110+
error_class=ValueError,
111+
)

0 commit comments

Comments
 (0)