closes: 10865 Fix muted exception#11804
Conversation
There was a problem hiding this comment.
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.
Agreed, also:
- Please put this test in
test_recwarn.pyinstead of creating a new file. - Add a
-> Nonetype annotation. - Add a docstring like
Check pytest.warns validates warning messages are strings (#10865).
nicoddemus
left a comment
There was a problem hiding this comment.
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.
| 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.
src/_pytest/recwarn.py
Outdated
| module=w.__module__, | ||
| source=w.source, | ||
| ) | ||
| # Check warnings has valid argument type |
There was a problem hiding this comment.
| # Check warnings has valid argument type | |
| # Check warnings has valid argument type (#10865). |
There was a problem hiding this comment.
Agreed, also:
- Please put this test in
test_recwarn.pyinstead of creating a new file. - Add a
-> Nonetype 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! |
|
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