|
1 | 1 | """Module to help with deprecating objects and classes
|
2 | 2 | """
|
| 3 | +from __future__ import annotations |
3 | 4 |
|
4 | 5 | import warnings
|
| 6 | +from typing import Type |
5 | 7 |
|
6 | 8 | from .deprecator import Deprecator
|
7 | 9 | from .pkg_info import cmp_pkg_version
|
@@ -77,3 +79,40 @@ class VisibleDeprecationWarning(UserWarning):
|
77 | 79 |
|
78 | 80 |
|
79 | 81 | 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()) |
0 commit comments