-
- Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. #57222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. #57222
Changes from 3 commits
83a045b 05bb67d f404767 da262ca d47bf11 9093962 74b93de 2242796 b8151ee 910cae6 7331302 a3b18ab e5020ca a21cfc9 ea25602 2fadce2 d13e876 cb5bef6 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -32,7 +32,8 @@ def assert_produces_warning( | |
| ] = "always", | ||
| check_stacklevel: bool = True, | ||
| raise_on_extra_warnings: bool = True, | ||
| match: str | None = None, | ||
| match: str | tuple[str | None, ...] | None = None, | ||
| must_find_all_warnings: bool = False, | ||
rhshadrach marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ) -> Generator[list[warnings.WarningMessage], None, None]: | ||
| """ | ||
| Context manager for running code expected to either raise a specific warning, | ||
| | @@ -68,8 +69,17 @@ class for all warnings. To raise multiple types of exceptions, | |
| raise_on_extra_warnings : bool, default True | ||
| Whether extra warnings not of the type `expected_warning` should | ||
| cause the test to fail. | ||
| match : str, optional | ||
| Match warning message. | ||
| match : {str, tuple[str, ...]}, optional | ||
| Match warning message. If it's a tuple, it has to be the size of | ||
| `expected_warning`. If additionally `must_find_all_warnings` is | ||
| True, each expected warning's message gets matched with a respective | ||
| match. Otherwise, multiple values get treated as an alternative. | ||
| must_find_all_warnings : bool, default False | ||
| If True and `expected_warning` is a tuple, each expected warning | ||
| type must get encountered. Otherwise, even one expected warning | ||
| results in success. | ||
| | ||
| .. versionadded:: 3.0.0 | ||
| ||
| | ||
| Examples | ||
| -------- | ||
| | @@ -99,13 +109,37 @@ class for all warnings. To raise multiple types of exceptions, | |
| yield w | ||
| finally: | ||
| if expected_warning: | ||
| expected_warning = cast(type[Warning], expected_warning) | ||
| _assert_caught_expected_warning( | ||
| caught_warnings=w, | ||
| expected_warning=expected_warning, | ||
| match=match, | ||
| check_stacklevel=check_stacklevel, | ||
| ) | ||
| if type(expected_warning) == tuple and must_find_all_warnings: | ||
| ||
| expected_warning = cast(tuple[type[Warning]], expected_warning) | ||
| match = ( | ||
| match | ||
| if type(match) == tuple | ||
| else tuple( | ||
| cast(str | None, match) | ||
| for i in range(len(expected_warning)) | ||
| ) | ||
| ) | ||
rhshadrach marked this conversation as resolved. Show resolved Hide resolved | ||
| for warning_type, warning_match in zip(expected_warning, match): | ||
| _assert_caught_expected_warning( | ||
| caught_warnings=w, | ||
| expected_warning=warning_type, | ||
| match=warning_match, | ||
| check_stacklevel=check_stacklevel, | ||
| ) | ||
| else: | ||
| expected_warning = cast(type[Warning], expected_warning) | ||
| ||
| match = cast( | ||
| str, | ||
| "|".join(m for m in match if m) | ||
| if type(match) == tuple | ||
| else match, | ||
| ) | ||
| _assert_caught_expected_warning( | ||
| caught_warnings=w, | ||
| expected_warning=expected_warning, | ||
| match=match, | ||
| check_stacklevel=check_stacklevel, | ||
| ) | ||
| if raise_on_extra_warnings: | ||
| _assert_caught_no_extra_warnings( | ||
| caught_warnings=w, | ||
| | ||
Uh oh!
There was an error while loading. Please reload this page.