|
24 | 24 | import unittest |
25 | 25 | import warnings |
26 | 26 | from collections.abc import Iterable |
| 27 | +from dataclasses import dataclass |
27 | 28 | from functools import wraps |
28 | 29 | from inspect import signature |
29 | 30 | from subprocess import STDOUT, CalledProcessError, TimeoutExpired, check_output |
|
49 | 50 | _in_unstable_openblas_configuration, |
50 | 51 | ) |
51 | 52 | from sklearn.utils._array_api import _check_array_api_dispatch |
52 | | -from sklearn.utils.fixes import parse_version, sp_version |
| 53 | +from sklearn.utils.fixes import VisibleDeprecationWarning, parse_version, sp_version |
53 | 54 | from sklearn.utils.multiclass import check_classification_targets |
54 | 55 | from sklearn.utils.validation import ( |
55 | 56 | check_array, |
@@ -1095,3 +1096,74 @@ def _array_api_for_tests(array_namespace, device): |
1095 | 1096 | if cupy.cuda.runtime.getDeviceCount() == 0: |
1096 | 1097 | raise SkipTest("CuPy test requires cuda, which is not available") |
1097 | 1098 | return xp |
| 1099 | + |
| 1100 | + |
| 1101 | +def _get_warnings_filters_info_list(): |
| 1102 | + @dataclass |
| 1103 | + class WarningInfo: |
| 1104 | + action: "warnings._ActionKind" |
| 1105 | + message: str = "" |
| 1106 | + category: type[Warning] = Warning |
| 1107 | + |
| 1108 | + def to_filterwarning_str(self): |
| 1109 | + if self.category.__module__ == "builtins": |
| 1110 | + category = self.category.__name__ |
| 1111 | + else: |
| 1112 | + category = f"{self.category.__module__}.{self.category.__name__}" |
| 1113 | + |
| 1114 | + return f"{self.action}:{self.message}:{category}" |
| 1115 | + |
| 1116 | + return [ |
| 1117 | + WarningInfo("error", category=DeprecationWarning), |
| 1118 | + WarningInfo("error", category=FutureWarning), |
| 1119 | + WarningInfo("error", category=VisibleDeprecationWarning), |
| 1120 | + # TODO: remove when pyamg > 5.0.1 |
| 1121 | + # Avoid a deprecation warning due pkg_resources usage in pyamg. |
| 1122 | + WarningInfo( |
| 1123 | + "ignore", |
| 1124 | + message="pkg_resources is deprecated as an API", |
| 1125 | + category=DeprecationWarning, |
| 1126 | + ), |
| 1127 | + WarningInfo( |
| 1128 | + "ignore", |
| 1129 | + message="Deprecated call to `pkg_resources", |
| 1130 | + category=DeprecationWarning, |
| 1131 | + ), |
| 1132 | + # pytest-cov issue https://github.com/pytest-dev/pytest-cov/issues/557 not |
| 1133 | + # fixed although it has been closed. https://github.com/pytest-dev/pytest-cov/pull/623 |
| 1134 | + # would probably fix it. |
| 1135 | + WarningInfo( |
| 1136 | + "ignore", |
| 1137 | + message=( |
| 1138 | + "The --rsyncdir command line argument and rsyncdirs config variable are" |
| 1139 | + " deprecated" |
| 1140 | + ), |
| 1141 | + category=DeprecationWarning, |
| 1142 | + ), |
| 1143 | + # XXX: Easiest way to ignore pandas Pyarrow DeprecationWarning in the |
| 1144 | + # short-term. See https://github.com/pandas-dev/pandas/issues/54466 for |
| 1145 | + # more details. |
| 1146 | + WarningInfo( |
| 1147 | + "ignore", |
| 1148 | + message=r"\s*Pyarrow will become a required dependency", |
| 1149 | + category=DeprecationWarning, |
| 1150 | + ), |
| 1151 | + ] |
| 1152 | + |
| 1153 | + |
| 1154 | +def get_pytest_filterwarning_lines(): |
| 1155 | + warning_filters_info_list = _get_warnings_filters_info_list() |
| 1156 | + return [ |
| 1157 | + warning_info.to_filterwarning_str() |
| 1158 | + for warning_info in warning_filters_info_list |
| 1159 | + ] |
| 1160 | + |
| 1161 | + |
| 1162 | +def turn_warnings_into_errors(): |
| 1163 | + warnings_filters_info_list = _get_warnings_filters_info_list() |
| 1164 | + for warning_info in warnings_filters_info_list: |
| 1165 | + warnings.filterwarnings( |
| 1166 | + warning_info.action, |
| 1167 | + message=warning_info.message, |
| 1168 | + category=warning_info.category, |
| 1169 | + ) |
0 commit comments