-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
closes: 10865 Fix muted exception #11804
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
Conversation
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 for volunteering to take this on, @whysage! The implementation looks good; I've just suggested a tweak to the message and a test approach which I hope will get us full coverage of the diff.
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.
I think we could simplify this test by running it inline, something like:
import warnings
import pytest
def test_raise_type_error_on_non_string_warning():
with pytest.raises(TypeError, match="Warning message must be str"):
with pytest.warns(UserWarning):
warnings.warn(1)
# Check that we get the same behavior with the stdlib, at least if filtering
# (see https://github.com/python/cpython/issues/103577 for details)
with pytest.raises(TypeError):
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(1)
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.
Agreed, also:
- Please put this test in
test_recwarn.py
instead of creating a new file. - Add a
-> None
type annotation. - Add a docstring like
Check pytest.warns validates warning messages are strings (#10865).
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, updated
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 @whysage!
Left a few comments as well. 👍
changelog/10865.bugfix.rst
Outdated
@@ -0,0 +1 @@ | |||
Fix a TypeError in a warning arguments call muted by warnings filter. |
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.
Fix a TypeError in a warning arguments call muted by warnings filter. | |
:func:`pytest.warns` now validates that warning object's ``message`` is of type `str` -- currently in Python it is possible to pass other types than `str` when creating `Warning` instances, however this causes an exception when :func:`warnings.filterwarnings` is used to filter those warnings. See `CPython #103577 <https://github.com/python/cpython/issues/103577>`__ for a discussion. | |
While this can be considered a bug in CPython, we decided to put guards in pytest as the error message produced without this check in place is confusing. |
However I'm not sure this should be a bugfix
, I would argue this is more of an improvement
.
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, updated
src/_pytest/recwarn.py
Outdated
@@ -330,3 +330,11 @@ def found_str(): | |||
module=w.__module__, | |||
source=w.source, | |||
) | |||
# Check warnings has valid argument type |
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.
# Check warnings has valid argument type | |
# Check warnings has valid argument type (#10865). |
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, updated
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.
Agreed, also:
- Please put this test in
test_recwarn.py
instead of creating a new file. - Add a
-> None
type annotation. - Add a docstring like
Check pytest.warns validates warning messages are strings (#10865).
HI! Any ideas how to fix this? |
0fdd065
to
269b674
Compare
0270181
to
79f934f
Compare
Hi all! |
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.
Looks great! Thanks for bearing with us through the process @whysage, we really appreciate your contribution 🥰
Thank you! |
* feat: 10865 * feat: 10865 refactor code and tests * feat: 10865 add test skip for pypy * feat: 10865 add test with valid warning * feat: 10865 fix v2 for codecov * feat: 10865 fix conflict
closes #10865