Context manager to ensure exceptions are raised within a code block. This is similar to and inspired from pytest.raises, but supports a few other cases. This is only intended to be used in estimator_checks.py where we don't want to use pytest. In the rest of the code base, just use
(expected_exc_type, match=None, may_pass=False, err_msg=None)
| 1073 | |
| 1074 | |
| 1075 | def raises(expected_exc_type, match=None, may_pass=False, err_msg=None): |
| 1076 | """Context manager to ensure exceptions are raised within a code block. |
| 1077 | |
| 1078 | This is similar to and inspired from pytest.raises, but supports a few |
| 1079 | other cases. |
| 1080 | |
| 1081 | This is only intended to be used in estimator_checks.py where we don't |
| 1082 | want to use pytest. In the rest of the code base, just use pytest.raises |
| 1083 | instead. |
| 1084 | |
| 1085 | Parameters |
| 1086 | ---------- |
| 1087 | excepted_exc_type : Exception or list of Exception |
| 1088 | The exception that should be raised by the block. If a list, the block |
| 1089 | should raise one of the exceptions. |
| 1090 | match : str or list of str, default=None |
| 1091 | A regex that the exception message should match. If a list, one of |
| 1092 | the entries must match. If None, match isn't enforced. |
| 1093 | may_pass : bool, default=False |
| 1094 | If True, the block is allowed to not raise an exception. Useful in |
| 1095 | cases where some estimators may support a feature but others must |
| 1096 | fail with an appropriate error message. By default, the context |
| 1097 | manager will raise an exception if the block does not raise an |
| 1098 | exception. |
| 1099 | err_msg : str, default=None |
| 1100 | If the context manager fails (e.g. the block fails to raise the |
| 1101 | proper exception, or fails to match), then an AssertionError is |
| 1102 | raised with this message. By default, an AssertionError is raised |
| 1103 | with a default error message (depends on the kind of failure). Use |
| 1104 | this to indicate how users should fix their estimators to pass the |
| 1105 | checks. |
| 1106 | |
| 1107 | Attributes |
| 1108 | ---------- |
| 1109 | raised_and_matched : bool |
| 1110 | True if an exception was raised and a match was found, False otherwise. |
| 1111 | """ |
| 1112 | return _Raises(expected_exc_type, match, may_pass, err_msg) |
| 1113 | |
| 1114 | |
| 1115 | class _Raises(contextlib.AbstractContextManager): |
no test coverage detected
searching dependent graphs…