-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Recwarn: Fix module when re-emitting unmatched warnings #12898
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix regression with :func:`pytest.warns` using wrong module when re-emitting unmatched warnings. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -545,6 +545,38 @@ def test_it(): | |
result.assert_outcomes() | ||
|
||
|
||
def test_re_emit_uses_correct_module(pytester: Pytester) -> None: | ||
warning_module_code = """ | ||
import warnings | ||
|
||
def trigger_warning(msg): | ||
warnings.warn(msg, UserWarning) | ||
""" | ||
pytester.makepyfile(module_a=warning_module_code) | ||
pytester.makepyfile(module_b=warning_module_code) | ||
|
||
test_code = """ | ||
import pytest | ||
import warnings | ||
from module_a import trigger_warning as trigger_warning_a | ||
from module_b import trigger_warning as trigger_warning_b | ||
|
||
def test_ignore_warning_from_module_a(): | ||
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"): | ||
with pytest.warns(UserWarning, match="module A.") as outer: | ||
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. The test is not really veryfing the new |
||
warnings.filterwarnings("ignore", category=UserWarning, module="module_a") | ||
with pytest.warns(UserWarning, match="module B.") as inner: # re-emit the module A warning | ||
trigger_warning_a("module A.") | ||
trigger_warning_b("module B.") | ||
""" | ||
# Write the test to a new file called 'test_re_emit.py' | ||
pytester.makepyfile(test_re_emit=test_code) | ||
|
||
# Run the test and assert that it passed | ||
result = pytester.runpytest() | ||
result.assert_outcomes(passed=1) | ||
|
||
|
||
def test_raise_type_error_on_invalid_warning() -> None: | ||
"""Check pytest.warns validates warning messages are strings (#10865) or | ||
Warning instances (#11959).""" | ||
|
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.
Nice! 👍