feat: 10865 refactor code and tests
This commit is contained in:
parent
62c6f53689
commit
4806b591fc
1
AUTHORS
1
AUTHORS
|
@ -416,6 +416,7 @@ Vivaan Verma
|
||||||
Vlad Dragos
|
Vlad Dragos
|
||||||
Vlad Radziuk
|
Vlad Radziuk
|
||||||
Vladyslav Rachek
|
Vladyslav Rachek
|
||||||
|
Volodymyr Kochetkov
|
||||||
Volodymyr Piskun
|
Volodymyr Piskun
|
||||||
Wei Lin
|
Wei Lin
|
||||||
Wil Cooley
|
Wil Cooley
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Fix a TypeError in a warning arguments call muted by warnings filter.
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
: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.
|
|
@ -329,11 +329,11 @@ class WarningsChecker(WarningsRecorder):
|
||||||
module=w.__module__,
|
module=w.__module__,
|
||||||
source=w.source,
|
source=w.source,
|
||||||
)
|
)
|
||||||
# Check warnings has valid argument type
|
# Check warnings has valid argument type (#10865).
|
||||||
wrn: warnings.WarningMessage
|
wrn: warnings.WarningMessage
|
||||||
for wrn in self:
|
for wrn in self:
|
||||||
if isinstance(wrn.message, Warning):
|
if isinstance(wrn.message, Warning):
|
||||||
if not isinstance(wrn.message.args[0], str):
|
if not isinstance(msg := wrn.message.args[0], str):
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
f"Warning message must be str, got {type(wrn.message.args[0])}"
|
f"Warning message must be str, got {msg!r} (type {type(msg).__name__})"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
from _pytest.pytester import Pytester
|
|
||||||
|
|
||||||
|
|
||||||
class TestWarningAttributes:
|
|
||||||
def test_raise_type_error(self, pytester: Pytester) -> None:
|
|
||||||
pytester.makepyfile(
|
|
||||||
"""
|
|
||||||
import pytest
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
def test_example_one():
|
|
||||||
with pytest.warns(UserWarning):
|
|
||||||
warnings.warn(1)
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
result = pytester.runpytest()
|
|
||||||
result.stdout.fnmatch_lines(["*1 failed*"])
|
|
|
@ -477,3 +477,17 @@ class TestWarns:
|
||||||
with pytest.raises(ValueError, match="some exception"):
|
with pytest.raises(ValueError, match="some exception"):
|
||||||
warnings.warn("some warning", category=FutureWarning)
|
warnings.warn("some warning", category=FutureWarning)
|
||||||
raise ValueError("some exception")
|
raise ValueError("some exception")
|
||||||
|
|
||||||
|
|
||||||
|
def test_raise_type_error_on_non_string_warning() -> None:
|
||||||
|
"""Check pytest.warns validates warning messages are strings (#10865)."""
|
||||||
|
with pytest.raises(TypeError, match="Warning message must be str"):
|
||||||
|
with pytest.warns(UserWarning):
|
||||||
|
warnings.warn(1) # type: ignore
|
||||||
|
|
||||||
|
# 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.filterwarnings("ignore", "test")
|
||||||
|
warnings.warn(1) # type: ignore
|
||||||
|
|
Loading…
Reference in New Issue