-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. #57222
Changes from 10 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 = True, | ||
) -> Generator[list[warnings.WarningMessage], None, None]: | ||
""" | ||
Context manager for running code expected to either raise a specific warning, | ||
|
@@ -68,8 +69,15 @@ 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 True | ||
If True and `expected_warning` is a tuple, each expected warning | ||
type must get encountered. Otherwise, even one expected warning | ||
results in success. | ||
|
||
Examples | ||
-------- | ||
|
@@ -97,13 +105,32 @@ 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 isinstance(expected_warning, tuple) and must_find_all_warnings: | ||
match = ( | ||
match | ||
if isinstance(match, tuple) | ||
else tuple(match for i in range(len(expected_warning))) | ||
) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can still be a tuple, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right. I believe this was originally done, to suppress |
||
match = ( | ||
"|".join(m for m in match if m) | ||
if isinstance(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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Use
(match,) * len(expected_warning)
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done