-
-
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?
Conversation
for more information, see https://pre-commit.ci
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.
Thanks @reaganjlee, we appreciate the PR!
Left a comment, but other than that we need a test to ensure this fix does not regress in the future. 👍
src/_pytest/recwarn.py
Outdated
module = next( | ||
k | ||
for k, v in sys.modules.items() | ||
if getattr(v, "__file__", None) == w.filename | ||
) |
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.
We should probably fallback to some string just in case something is very wrong and we cannot match the filename:
module = next( | |
k | |
for k, v in sys.modules.items() | |
if getattr(v, "__file__", None) == w.filename | |
) | |
module = next( | |
k | |
for k, v in sys.modules.items() | |
if getattr(v, "__file__", None) == w.filename, | |
None | |
) |
I'm assuming None
is a valid argument for module=
.
@@ -0,0 +1 @@ | |||
Fix regression with :func:`pytest.warns` using wrong module when re-emitting unmatched warnings. |
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! 👍
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The test is not really veryfing the new module
attribute... if I go in the code and set module = None
in warnings.warn_explicit
, the test still passes, which makes it unsuitable for a regression test... can you improve the test to check the actual module
attribute of the warnings?
Fixes #11933
Credit to @Zac-HD for fix!